From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from integral2.. (unknown [180.242.99.67]) by gnuweeb.org (Postfix) with ESMTPSA id 975F47E465; Thu, 19 May 2022 17:21:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1652980906; bh=e3Z1rLbTrDyV0Qy1q6LaHjUA/vCayqRtvdmXJu+gLAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=clzRgnDvILoQDmPoRsRlD4wK1giE6tkRIp07qlc0w0qXq5UcSmGqUkdyyvTu5hLrP 8LiZVJYr1K0XXULIl8DVSb/XksuAMXikboMtTOilQmtJ3cGpKYFClNUJUFIKS4yHsa iv7C+n6ZhhjOpI5ccpXbM7H2zovVXot7RixyvqywhwggY+wfFQQJL76K86pdFcd0Ua VG4MEtDesqjvEFbZRlSmX0MEIVJa4aHEX0gu78qxcs2GA0J/lTSh0G1dXS0s10QtCZ XeExqByaex1aDR3GaNBAsw6retgUmX8fiYWE46bAjh90eLUTUiMmlVJ0nfQTIu3+DG Jq0cz6PIpttMQ== From: Ammar Faizi To: "Paul E. McKenney" , Willy Tarreau Cc: Ammar Faizi , Alviro Iskandar Setiawan , Linux Kernel Mailing List , GNU/Weeb Mailing List , Facebook Kernel Team Subject: [PATCH v1 1/2] tools/nolibc/stdlib: Support overflow checking for older compiler versions Date: Fri, 20 May 2022 00:21:15 +0700 Message-Id: <20220519172116.283687-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20220519172116.283687-1-ammarfaizi2@gnuweeb.org> References: <20220519172116.283687-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Previously, we used __builtin_mul_overflow() to check for overflow in the multiplication operation in the calloc() function. However, older compiler versions don't support this built-in. This patch changes the overflow checking mechanism to make it work on any compiler version by using a division method to check for overflow. No functional change intended. While in there, remove the unused variable `void *orig`. Link: https://lore.kernel.org/lkml/20220330024114.GA18892@1wt.eu Suggested-by: Willy Tarreau Cc: Alviro Iskandar Setiawan Signed-off-by: Ammar Faizi --- tools/include/nolibc/stdlib.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index 8fd32eaf8037..92378c4b9660 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -128,10 +128,9 @@ void *malloc(size_t len) static __attribute__((unused)) void *calloc(size_t size, size_t nmemb) { - void *orig; - size_t res = 0; + size_t x = size * nmemb; - if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) { + if (__builtin_expect(size && ((x / size) != nmemb), 0)) { SET_ERRNO(ENOMEM); return NULL; } @@ -140,7 +139,7 @@ void *calloc(size_t size, size_t nmemb) * No need to zero the heap, the MAP_ANONYMOUS in malloc() * already does it. */ - return malloc(res); + return malloc(x); } static __attribute__((unused)) -- Ammar Faizi