summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorThomas Weißschuh <linux@weissschuh.net>2026-04-04 14:50:19 +0300
committerThomas Weißschuh <linux@weissschuh.net>2026-04-06 20:46:52 +0300
commit1e3c374e9fd5ef0bf1ebcb866505b1aad404959e (patch)
treed6aa1b6831398bf62f34dd0d5d90ad419c6b0b5e /tools
parent12496aad10c5671d66e160487326de942cd440ba (diff)
downloadlinux-1e3c374e9fd5ef0bf1ebcb866505b1aad404959e.tar.xz
tools/nolibc: check for overflow in calloc() without divisions
On some architectures without native division instructions the division can generate calls into libgcc/compiler-rt. This library might not be available, so its use should be avoided. Use the compiler builtin to check for overflows without needing a division. The builtin has been available since GCC 3 and clang 3.8. Signed-off-by: Thomas Weißschuh <linux@weissschuh.net> Acked-by: Willy Tarreau <w@1wt.eu> Link: https://patch.msgid.link/20260404-nolibc-asprintf-v2-1-17d2d0df9763@weissschuh.net
Diffstat (limited to 'tools')
-rw-r--r--tools/include/nolibc/stdlib.h4
1 files changed, 2 insertions, 2 deletions
diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h
index 2113a8e7695d..1816c2368b68 100644
--- a/tools/include/nolibc/stdlib.h
+++ b/tools/include/nolibc/stdlib.h
@@ -145,9 +145,9 @@ void *malloc(size_t len)
static __attribute__((unused))
void *calloc(size_t size, size_t nmemb)
{
- size_t x = size * nmemb;
+ size_t x;
- if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
+ if (__builtin_expect(__builtin_mul_overflow(size, nmemb, &x), 0)) {
SET_ERRNO(ENOMEM);
return NULL;
}