Re: undefined behavior itoa

Corinna Vinschen <[email protected]> Thu, 12 Feb 2026 21:23:57 +0100
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hi Johannes,

thanks, but your patch doesn't apply cleanly.  Can you please send
this as a git patch created with git format-patch, and if your mailer
breks whitespaces (as it appears to), please send the patch as plain-text
attachment (you may want to drop html entirely).


Thanks,
Corinna


On Feb 12 16:55, Johannes Hack wrote:
> Hello,
> I believe I have found undefined behavior in |newlib/libc/stdlib/itoa.c|
> when handling |INT_MIN.|
> Best regards
> Johannes
> ```
> diff --git a/newlib/libc/stdlib/itoa.c b/newlib/libc/stdlib/itoa.c
> index 7a7daf0ce..9271ec443 100644
> --- a/newlib/libc/stdlib/itoa.c
> +++ b/newlib/libc/stdlib/itoa.c
> @@ -49,7 +49,7 @@ __itoa (int value,
> if ((base == 10) && (value < 0))
> {
> str[i++] = '-';
> - uvalue = (unsigned)-value;
> + uvalue = -(unsigned)value;
> }
> else
> uvalue = (unsigned)value;
> ```