Re: [PATCH 2/2] vsprintf: Stop using obsolete simple_strtoul()

Andy Shevchenko
2018-12-11T21:30:39Z

On Tue, Dec 11, 2018 at 10:19:08AM -0800, Linus Torvalds wrote:
> On Tue, Dec 11, 2018 at 10:05 AM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > I think it's slightly more complicated, I run the following test case on glibc:
> >
> > uint32_t hi, lo, t;
> >
> > sscanf("00fafafafa0d0b0b0b0c000000", "%8x%8x%x", &hi, &lo, &t);
> >
> > 64-bit:
> > HI: 00fafafa LO: fa0d0b0b (c000000)
> > 32-bit:
> > HI: 00fafafa LO: fa0d0b0b (ffffffff)
>
> But that's exactly the values my pseudo-code gets (well, my
> "pseudo-code obviously just said
>
> // Now do "sign" and range checking on val
>
> The three sub-parts are: "00fafafa" "fa0d0b0b" and "0b0c000000"
>
> and the third one encounters an overflow in "long" on 32-bit, so it
> turns into ~0.
>
> And yes, the 64-bit "long" in that third value gets truncated to
> "uint32" when written to "t" (which is wht that "0b" part just gets
> lost.
>
> And that's just because of historical C scanf behavior. There's no
> overflow checking in "int". Only in "long" and "long long".

Yes, that's what my test case showed.

So, whatever implementation would be done, I think it is a good time
to introduce some test cases.

P.S. I have briefly checked and didn't find a single test case for scanf().
Maybe I looked to the wrong module(s), but test_printf.c might be more or less a good choice.

--
With Best Regards,
Andy Shevchenko