From nobody Thu May 7 19:54:38 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1F423C433F5 for ; Thu, 19 May 2022 17:21:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242529AbiESRVw (ORCPT ); Thu, 19 May 2022 13:21:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242523AbiESRVs (ORCPT ); Thu, 19 May 2022 13:21:48 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F336A2046 for ; Thu, 19 May 2022 10:21:47 -0700 (PDT) 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 Acked-by: Willy Tarreau Reviewed-by: Alviro Iskandar Setiawan --- 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 =3D 0; + size_t x =3D size * nmemb; =20 - if (__builtin_expect(__builtin_mul_overflow(nmemb, size, &res), 0)) { + if (__builtin_expect(size && ((x / size) !=3D 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); } =20 static __attribute__((unused)) --=20 Ammar Faizi From nobody Thu May 7 19:54:38 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3275CC433F5 for ; Thu, 19 May 2022 17:21:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242557AbiESRV5 (ORCPT ); Thu, 19 May 2022 13:21:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242539AbiESRVu (ORCPT ); Thu, 19 May 2022 13:21:50 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C647EA205D for ; Thu, 19 May 2022 10:21:49 -0700 (PDT) Received: from integral2.. (unknown [180.242.99.67]) by gnuweeb.org (Postfix) with ESMTPSA id 6CFA17E46C; Thu, 19 May 2022 17:21:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1652980909; bh=U+32yynyQuIUhRnsYtaxv6b9DYu44J416tkLENqnFlk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EW60Ay+9+/uG7TipPtUBtVq+c9U3PA8bNcrfVDF6vIT/QTXkb6KGnF5RgyOJgoaBC HaJ+nuWxSScNhZjpRK9Ufmo6f/DvfJ8qvlj1zoV5qtfnXh6+mojxEqxSWoiS7Fnuud swCOWOfBMDFFJOI+EiGz/vMK7JJsneHIDFTt3paUfce64yiTbV39x9WuW+9nHfb0ya uq1JW9EEliKT9HfLg7cuOBzt8sW4rqSu3oS4o9oUqL7bxNZrnNR9XdQKKja2SRoIX8 iVVnZiSxlfqsKsDFW66E/G0wRHZPPyQj5N/j7q8E/1g8JUO84kXlKmTRqYMw0HyjA2 rx/sPmqALsbEg== From: Ammar Faizi To: "Paul E. McKenney" , Willy Tarreau Cc: Alviro Iskandar Setiawan , Ammar Faizi , Linux Kernel Mailing List , GNU/Weeb Mailing List , Facebook Kernel Team Subject: [PATCH v1 2/2] tools/nolibc/stdio: Add format attribute to enable printf warnings Date: Fri, 20 May 2022 00:21:16 +0700 Message-Id: <20220519172116.283687-3-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Alviro Iskandar Setiawan When we use printf and fprintf functions from the nolibc, we don't get any warning from the compiler if we have the wrong arguments. For example, the following calls will compile silently: ``` printf("%s %s\n", "aaa"); fprintf(stdout, "%s %s\n", "xxx", 1); ``` (Note the wrong arguments). Those calls are undefined behavior. The compiler can help us warn about the above mistakes by adding a `printf` format attribute to those functions declaration. This patch adds it, and now it yields these warnings for those mistakes: ``` warning: format `%s` expects a matching `char *` argument [-Wformat=3D] warning: format `%s` expects argument of type `char *`, but argument 4 ha= s type `int` [-Wformat=3D] ``` [ ammarfaizi2: Simplify the attribute placement. ] Signed-off-by: Alviro Iskandar Setiawan Signed-off-by: Ammar Faizi Acked-by: Willy Tarreau --- tools/include/nolibc/stdio.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 15dedf8d0902..a3cebc4bc3ac 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -273,7 +273,7 @@ int vfprintf(FILE *stream, const char *fmt, va_list arg= s) return written; } =20 -static __attribute__((unused)) +static __attribute__((unused, format(printf, 2, 3))) int fprintf(FILE *stream, const char *fmt, ...) { va_list args; @@ -285,7 +285,7 @@ int fprintf(FILE *stream, const char *fmt, ...) return ret; } =20 -static __attribute__((unused)) +static __attribute__((unused, format(printf, 1, 2))) int printf(const char *fmt, ...) { va_list args; --=20 Ammar Faizi