From patchwork Mon Jun 20 23:13:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Paul E. McKenney" X-Patchwork-Id: 12888563 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 2409ACCA480 for ; Mon, 20 Jun 2022 23:17:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346824AbiFTXRp (ORCPT ); Mon, 20 Jun 2022 19:17:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347094AbiFTXRP (ORCPT ); Mon, 20 Jun 2022 19:17:15 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DBBE222AA; Mon, 20 Jun 2022 16:13:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BEEB961515; Mon, 20 Jun 2022 23:13:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B9FAC3411C; Mon, 20 Jun 2022 23:13:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655766810; bh=PFW+nJeUjF0+z9b2ZiIQ19Ctf2Uib8uU1+Ln6e2nhDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hwiGTaw0+1uiz5RGyeKLUrv4A701/KRlUqZwqmCI5im1OvrE4Hl6ZA2ZbDBmurscf UL02wlNnUKAjN947vHf6bzqgrfN3bGr93Tv923/uXB1d6LDN+mgxSUO1wddxBF8Rep pt5vB3/27ynR6ecKJoIj1M+rQvyJIUd9TMIXE/pBuYRPNSnG6AuCvJQiMoxtwuddCt VIY/XlpbgcqwkgvbRxk1evIZCbdMsVcH8hUCk0WCLO+TX7MidGraZx3snt2yN/1RO+ NND9mWBkeGRHNxnqGQ8DddPZo03Vmj0aK1OyBU1qZtjAyZ9wjaUNGR+T9CpzD4V412 7PLYDXfuufBhg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id DB69C5C05B9; Mon, 20 Jun 2022 16:13:29 -0700 (PDT) From: "Paul E. McKenney" To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com, rostedt@goodmis.org, Ammar Faizi , Willy Tarreau , Alviro Iskandar Setiawan , "Paul E . McKenney" Subject: [PATCH rcu 1/5] tools/nolibc/stdlib: Support overflow checking for older compiler versions Date: Mon, 20 Jun 2022 16:13:24 -0700 Message-Id: <20220620231328.3845126-1-paulmck@kernel.org> X-Mailer: git-send-email 2.31.1.189.g2e36527f23 In-Reply-To: <20220620231325.GA3845036@paulmck-ThinkPad-P17-Gen-1> References: <20220620231325.GA3845036@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: rcu@vger.kernel.org From: Ammar Faizi 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 Signed-off-by: Paul E. McKenney --- 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 8fd32eaf80370..92378c4b96605 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))