From: Thomas Weißschuh Date: Sat, 19 Apr 2025 10:46:22 +0000 (+0200) Subject: tools/nolibc: fix integer overflow in i{64,}toa_r() and X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=4d231a7df1a85c7572b67a4666cb73adb977fbf6;p=users%2Fdwmw2%2Flinux.git tools/nolibc: fix integer overflow in i{64,}toa_r() and In twos complement the most negative number can not be negated. Fixes: b1c21e7d99cd ("tools/nolibc/stdlib: add i64toa() and u64toa()") Fixes: 66c397c4d2e1 ("tools/nolibc/stdlib: replace the ltoa() function with more efficient ones") Signed-off-by: Thomas Weißschuh Acked-by: Willy Tarreau Link: https://lore.kernel.org/r/20250419-nolibc-ubsan-v2-5-060b8a016917@weissschuh.net --- diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 86ad378ab1ea2..32b3038002c16 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -275,7 +275,7 @@ int itoa_r(long in, char *buffer) int len = 0; if (in < 0) { - in = -in; + in = -(unsigned long)in; *(ptr++) = '-'; len++; } @@ -411,7 +411,7 @@ int i64toa_r(int64_t in, char *buffer) int len = 0; if (in < 0) { - in = -in; + in = -(uint64_t)in; *(ptr++) = '-'; len++; }