From mboxrd@z Thu Jan 1 00:00:00 1970 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 C43CEC433F5 for ; Fri, 20 May 2022 04:19:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234571AbiETETf (ORCPT ); Fri, 20 May 2022 00:19:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231559AbiETET2 (ORCPT ); Fri, 20 May 2022 00:19:28 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5C449EBAA0 for ; Thu, 19 May 2022 21:19:27 -0700 (PDT) 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) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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