Re: [PATCH v3 1/4] lib: vsprintf: scanf: Negative number must have field width > 1

From: Petr Mladek
Date: Mon Dec 21 2020 - 10:16:50 EST


On Thu 2020-12-17 18:00:54, Richard Fitzgerald wrote:
> If a signed number field starts with a '-' the field width must be > 1,
> or unlimited, to allow at least one digit after the '-'.
>
> This patch adds a check for this. If a signed field starts with '-'
> and field_width == 1 the scanf will quit.
>
> It is ok for a signed number field to have a field width of 1 if it
> starts with a digit. In that case the single digit can be converted.

The change makes perfect sense. vsscanf() should always process only one
character when the field width is 1.

Well, it has a potential to break existing users that rely on the
broken behavior. Fortunately, there seems be only one:

drivers/net/wireless/intel/iwlegacy/3945-mac.c: if (sscanf(buf, "%1i", &ant) != 1) {

It is used to set a device parameter: il3945_mod_params.antenna.
There are three valid values:

enum il3945_antenna {
IL_ANTENNA_DIVERSITY,
IL_ANTENNA_MAIN,
IL_ANTENNA_AUX
};

So, we should be on the safe side.

Anyway, adding people from
get_maintainer.pl drivers/net/wireless/intel/iwlegacy/3945-mac.c
so that they are aware of this.

> Signed-off-by: Richard Fitzgerald <rf@xxxxxxxxxxxxxxxxxxxxx>

Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>

Best Regards,
Petr

> ---
> lib/vsprintf.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index 14c9a6af1b23..8954ff94a53c 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -3433,8 +3433,12 @@ int vsscanf(const char *buf, const char *fmt, va_list args)
> str = skip_spaces(str);
>
> digit = *str;
> - if (is_sign && digit == '-')
> + if (is_sign && digit == '-') {
> + if (field_width == 1)
> + break;
> +
> digit = *(str + 1);
> + }
>
> if (!digit
> || (base == 16 && !isxdigit(digit))
> --
> 2.20.1