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

Linus Torvalds
2018-12-11T18:19:31Z

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".

Linus