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

From: Andy Shevchenko

Date: Thu Mar 26 2026 - 06:20:27 EST


On Wed, Mar 25, 2026 at 10:27:41PM +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.

...

> +static void
> +set_field_width(struct printf_spec *spec, int width)

One line.

static void set_field_width(struct printf_spec *spec, int width)

> +{
> + spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
> + WARN_ONCE(spec->field_width != width, "field width %d out of range",
> + width);

I would also make it one line for readability.

WARN_ONCE(spec->field_width != width, "field width %d out of range", width);

> +}
> +
> +static void
> +set_precision(struct printf_spec *spec, int prec)

One line

static void set_precision(struct printf_spec *spec, int prec)

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

--
With Best Regards,
Andy Shevchenko