Re: [PATCH] lib/vsprintf: Fix to check field_width and precision

From: Andy Shevchenko

Date: Thu Mar 19 2026 - 03:11:37 EST


On Thu, Mar 19, 2026 at 09:26:59AM +0900, Masami Hiramatsu (Google) wrote:

> Check the field_width and presition correctly. Previously it depends
> on the bitfield conversion from int to check out-of-range error.
> However, commit 938df695e98d ("vsprintf: associate the format state
> with the format pointer") changed those fields to int.
> We need to check the out-of-range correctly without bitfield
> conversion.

...

> spec->field_width = width;
> - if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
> + if (WARN_ONCE(spec->field_width > FIELD_WIDTH_MAX ||
> + spec->field_width < -FIELD_WIDTH_MAX, "field width %d too large", width)) {

Also use logical split as below:

if (WARN_ONCE(spec->field_width > FIELD_WIDTH_MAX || spec->field_width < -FIELD_WIDTH_MAX,
"field width %d too large", width)) {

> spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
> }
> }

...

> spec->precision = prec;
> - if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
> + if (WARN_ONCE(spec->precision > PRECISION_MAX || spec->precision < 0,
> + "precision %d too large", prec)) {
> spec->precision = clamp(prec, 0, PRECISION_MAX);
> }

--
With Best Regards,
Andy Shevchenko