undefined behavior itoa

Message ID 217297ed-d56d-45c4-a1ec-d0e4f758cfdc@posteo.de
State New
Headers
Series undefined behavior itoa |

Commit Message

Johannes Hack Feb. 12, 2026, 4:55 p.m. UTC
  Hello,
I believe I have found undefined behavior in |newlib/libc/stdlib/itoa.c| 
when handling |INT_MIN.|
Best regards
Johannes
```
```
  

Comments

Corinna Vinschen Feb. 12, 2026, 8:23 p.m. UTC | #1
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;
> ```
  

Patch

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;