lib/vsprintf: Fix to check field_width and precision
Subsystems (2)
PATCH 1/1 lib/vsprintf: Fix to check field_width and precision
From: Masami Hiramatsu (Google) <[email protected]>
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.
--- lib/vsprintf.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
lib/vsprintf.c +4 -2
@@ -2803,7 +2803,8 @@ static void set_field_width(struct printf_spec *spec, int width) { 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)) { spec->field_width =clamp (width, -FIELD_WIDTH_MAX ,FIELD_WIDTH_MAX ); } }
@@ -2812,7 +2813,8 @@ static void set_precision(struct printf_spec *spec, int prec) { 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 ); } }
Andy Shevchenko
reviewerQuoted 6 lines · Masami Hiramatsu (Google)
Check the
field_widthand presition correctly. Previously it depends on the bitfield conversion from int to check out-of-range error. However, commit938df695e98d("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.
...
lib/vsprintf.c static void · line 2807
Quoted 4 lines
lib/vsprintf.c +2
-1spec->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)) {Quoted 3 lines
lib/vsprintf.c +0
-0spec->field_width =clamp (width, -FIELD_WIDTH_MAX ,FIELD_WIDTH_MAX ); } }
...
Quoted 6 lines
lib/vsprintf.c +2
-1spec->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 ); }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 18 lines · On Thu, 19 Mar 2026 09:11:21 +0200 Andy Shevchenko…
Quoted 6 lines · Masami Hiramatsu (Google)
Check the
field_widthand presition correctly. Previously it depends on the bitfield conversion from int to check out-of-range error. However, commit938df695e98d("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....
lib/vsprintf.c static void · line 2807
Quoted 4 lines
lib/vsprintf.c +2
-1spec->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:
Quoted 2 lines
if (WARN_ONCE(spec->field_width > FIELD_WIDTH_MAX || spec->field_width < -FIELD_WIDTH_MAX,
"field width %d too large", width)) {OK, let me update it.
Thanks,
Quoted 19 lines
Quoted 4 lines
lib/vsprintf.c +0
-0spec->field_width =clamp (width, -FIELD_WIDTH_MAX ,FIELD_WIDTH_MAX ); } }...
Quoted 6 lines
lib/vsprintf.c +2
-1spec->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 ); }Signature · 2 lines
With Best Regards, Andy Shevchenko
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>