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.0 required=5.0 tests=SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by gnuweeb.org (Postfix) with ESMTP id 2300A7E2D2 for ; Fri, 20 May 2022 04:19:25 +0000 (UTC) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 24K4JFAk005135; Fri, 20 May 2022 06:19:15 +0200 Date: Fri, 20 May 2022 06:19:15 +0200 From: Willy Tarreau To: Ammar Faizi Cc: "Paul E. McKenney" , Alviro Iskandar Setiawan , Linux Kernel Mailing List , GNU/Weeb Mailing List , Facebook Kernel Team Subject: Re: [PATCH v1 1/2] tools/nolibc/stdlib: Support overflow checking for older compiler versions Message-ID: <20220520041915.GC5001@1wt.eu> References: <20220519172116.283687-1-ammarfaizi2@gnuweeb.org> <20220519172116.283687-2-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220519172116.283687-2-ammarfaizi2@gnuweeb.org> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: Hi Ammar, On Fri, May 20, 2022 at 12:21:15AM +0700, Ammar Faizi wrote: > 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)) { Ah, that approach is even better than mine, I'm seeing that on x86 the compiler simply checks the overflow flag after the multiply, that's perfect! Acked-by: Willy Tarreau Willy