Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1769709iob; Thu, 19 May 2022 14:12:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz85yCY8gSSSmWiPh+kbNI0T/61zxU3mswC92qlSoYdKNOQcNDgga77fUykWmhS9qkJXP4y X-Received: by 2002:a05:6402:27cd:b0:427:bf42:44cb with SMTP id c13-20020a05640227cd00b00427bf4244cbmr7567887ede.276.1652994721182; Thu, 19 May 2022 14:12:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652994721; cv=none; d=google.com; s=arc-20160816; b=Pq17AiNJlgMSL+EommSU7rGXfUmTa9XzL02eCT1It4G+ZidD2z/PpMbownuwERPvL6 0H7eJmaoPaYbvWZiRaJoftML8OITNa0+VgdeRX/qElaXhq/L8gLFRpanfXkhU8ML75gz diJHSVuzDv2P96b7GyHaIk+5pvL7H85cpqqtQLUmpO67acVGktfSQ3TVTWugHSws8klV YGF6UFt328MnRu5PtbTqqAMuGZUB8Ucp6/4+U/quYDbm/glFIKfdsU/5/ryZRsqZQksz T8mHA0bUz6yDPGHfkw3Af47rvZ7pJrtOBscCOorvXDFH7QRrqCXV5/X4T/RWZNWUVf1v Kh5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iYjfxndw8lrs4GwSq9+bjYcHB3maGZCafWxLvBWV6ws=; b=iFr7eEel7e8DbyVTwjdx3qDLQfdLaAXpuXT5+Iid0B0E5H86tzumYm9YBo2S+cC8bh 3xEyYbOeJpu1uKsUvhSG1pLFbi8fC/3CKSBw6u+QdOLz8HPrcQMWxL4icCdIeoWcY0QO WbqoX+yhw2wt+4lK7me3tJd5mP6L5MpcgBXU0FtccKNPDzqVaOMlyw7HaPuO+40lyeIH FHPCZIow80FUXfXpbXxaJpAvQiC2sJW9bN8fJfJGCMTQsh+w019f5XLyGsKfqqNyU1pk 1F3JWNcSmbD5eboN2WVtImGMNsNK233LMohBLxzUSGHQxFIk8uT32ZS7W1iP+Kg0FYpN OOrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=clzRgnDv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v20-20020a056402349400b00427ea19fdc7si7639542edc.12.2022.05.19.14.11.34; Thu, 19 May 2022 14:12:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gnuweeb.org header.s=default header.b=clzRgnDv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnuweeb.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242531AbiESRVv (ORCPT + 99 others); Thu, 19 May 2022 13:21:51 -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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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