Re: [PATCH v8 5/7] rust: str: add radix prefixed integer parsing functions
From: Miguel Ojeda
Date: Thu Mar 20 2025 - 16:22:04 EST
On Thu, Feb 27, 2025 at 3:39 PM Andreas Hindborg <a.hindborg@xxxxxxxxxx> wrote:
>
> + // SAFETY: We checked that `val` will fit in `Self` above.
> + let val: Self = unsafe { val.try_into().unwrap_unchecked() };
This is wrong -- `val` can be the maximum, and thus it does not fit
since it is 2's complement, even if later the complement would.
In fact, it is caught by the doctest when run with debug assertions enabled:
/// assert_eq!(Ok(-128), i8::from_str(b_str!("-128")));
We try to put 128 into `i8`, which of course does not work...
Cheers,
Miguel