Re: [PATCH 4/4] lib/vsprintf: add missing (u8) cast in format_decode() lookup

Josh Law
2026-03-31T14:44:35Z

---- On Tue, 31 Mar 2026 15:33:53 +0100 pmladek@suse.com wrote ----

> On Tue 2026-03-24 22:49:40, Josh Law wrote:
> > The first lookup into the format_state table correctly casts to (u8)
> > at line 2778, but the second lookup after consuming a length qualifier
> > does not. On signed-char platforms, a byte >= 0x80 sign-extends to a
> > negative index, reading before the array.
> >
> > Add the same (u8) cast for consistency.
> >
> > Signed-off-by: Josh Law
>
> > ---
> > lib/vsprintf.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> > index 2758096b6f53..3108823e8c22 100644
> > --- a/lib/vsprintf.c
> > +++ b/lib/vsprintf.c
> > @@ -2783,7 +2783,7 @@ struct fmt format_decode(struct fmt fmt, struct
> printf_spec *spec)
> >             fmt.str++;
> >         }
> >         fmt.str++;
> > -        p = lookup_state + *fmt.str;
> > +        p = lookup_state + (u8)*fmt.str;
> >     }
> >     if (p->state) {
> >         if (p->base)
>
> This makes sense. Even though the current code is safe as pointed
> out by Andy.
>
> Reviewed-by: Petr Mladek
>
> Best Regards,
> Petr

Yeah, better safe than sorry in my opinion.

Thanks for the review petr!