Re: [PATCH v3 1/2] lib/vsprintf: Fix to check field_width and precision
From: David Laight
Date: Wed Mar 25 2026 - 05:23:16 EST
On Wed, 25 Mar 2026 10:17:04 +0900
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx> wrote:
> On Wed, 25 Mar 2026 09:33:20 +0900
> Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx> wrote:
>
> > On Tue, 24 Mar 2026 17:24:33 +0000
> > David Laight <david.laight.linux@xxxxxxxxx> wrote:
> >
> > > On Tue, 24 Mar 2026 17:45:49 +0100
> > > Petr Mladek <pmladek@xxxxxxxx> wrote:
> > >
> > > > On Mon 2026-03-23 13:59:05, David Laight wrote:
> > > ...
> > > > > I've also just fixed nolibc's handling of %*.*s (which is in 'next' since
> > > > > I only wrote it recently), the above is actually broken.
> > > > > Negative 'precision' (all values) are fine, they just request the default.
> > > >
> > > > Great catch! We should clamp the precision to (0, PRECISION_MAX). But we
> > > > should warn only when it is outside of (-PRECISION_MAX, PRECISION_MAX).
> > >
> > > No, you are going to clamp it needs to be to (-1, PRECISION_MAX);
> > > but max(precision, PRECISION_MAX) if fine now the value is being saved
> > > in an int.
> > > And there is no need to warn about changing negative values - they
> > > all mean exactly the same thing.
> >
> > Ah, indeed.
> >
> > "A negative precision is taken as if the precision were omitted."
>
> Hmm, and format_decode() does not accept -1 precision. We need
> another patch to fix it.
>
> spec->precision = -1;
> if (unlikely(*fmt.str == '.')) {
> fmt.str++;
> if (isdigit(*fmt.str)) { <---- isdigit() accepts '0'-'9', not '-'.
> spec->precision = skip_atoi(&fmt.str); <--- this only returns positive value.
> if (spec->precision < 0)
> spec->precision = 0;
That is fine, you aren't allowed a negative value there - just for "%.*d".
David
> } else if (*fmt.str == '*') {
>
> Thanks,
>