From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from elaine.keithp.com (home.keithp.com [63.227.221.253]) by sourceware.org (Postfix) with ESMTPS id 8C0E13857C47 for ; Tue, 11 Aug 2020 23:05:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8C0E13857C47 Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id A92793F2CB06 for ; Tue, 11 Aug 2020 16:05:47 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at keithp.com Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id mo5EpEJYYsSA; Tue, 11 Aug 2020 16:05:47 -0700 (PDT) Received: from keithp.com (koto.keithp.com [10.0.0.2]) by elaine.keithp.com (Postfix) with ESMTPSA id 3F3E63F2CA60; Tue, 11 Aug 2020 16:05:47 -0700 (PDT) Received: by keithp.com (Postfix, from userid 1000) id 24FE615821C0; Tue, 11 Aug 2020 16:05:47 -0700 (PDT) From: Keith Packard To: newlib@sourceware.org Subject: [PATCH 1/4] libc/stdlib: Use __builtin_mul_overflow for reallocarray and calloc Date: Tue, 11 Aug 2020 16:05:40 -0700 Message-Id: <20200811230543.2169774-2-keithp@keithp.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200811230543.2169774-1-keithp@keithp.com> References: <20200811230543.2169774-1-keithp@keithp.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Aug 2020 23:05:49 -0000 This built-in function (available in both gcc and clang) is more efficient and generates shorter code than open-coding the test. Signed-off-by: Keith Packard --- newlib/libc/stdlib/mallocr.c | 8 +++++++- newlib/libc/stdlib/nano-mallocr.c | 12 ++++++++++-- newlib/libc/stdlib/reallocarray.c | 17 +++++------------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/newlib/libc/stdlib/mallocr.c b/newlib/libc/stdlib/mallocr.c index 26d1c89cc..9ad720ada 100644 --- a/newlib/libc/stdlib/mallocr.c +++ b/newlib/libc/stdlib/mallocr.c @@ -3194,7 +3194,7 @@ Void_t* cALLOc(RARG n, elem_size) RDECL size_t n; size_t elem_size; mchunkptr p; INTERNAL_SIZE_T csz; - INTERNAL_SIZE_T sz = n * elem_size; + INTERNAL_SIZE_T sz; #if MORECORE_CLEARS mchunkptr oldtop; @@ -3202,6 +3202,12 @@ Void_t* cALLOc(RARG n, elem_size) RDECL size_t n; size_t elem_size; #endif Void_t* mem; + if (__builtin_mul_overflow((INTERNAL_SIZE_T) n, (INTERNAL_SIZE_T) elem_size, &sz)) + { + errno = ENOMEM; + return 0; + } + /* check if expand_top called, in which case don't need to clear */ #if MORECORE_CLEARS MALLOC_LOCK; diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c index 13b72c99f..04465eb9e 100644 --- a/newlib/libc/stdlib/nano-mallocr.c +++ b/newlib/libc/stdlib/nano-mallocr.c @@ -445,8 +445,16 @@ void nano_cfree(RARG void * ptr) * Implement calloc simply by calling malloc and set zero */ void * nano_calloc(RARG malloc_size_t n, malloc_size_t elem) { - void * mem = nano_malloc(RCALL n * elem); - if (mem != NULL) memset(mem, 0, n * elem); + ptrdiff_t bytes; + void * mem; + + if (__builtin_mul_overflow (n, elem, &bytes)) + { + RERRNO = ENOMEM; + return NULL; + } + mem = nano_malloc(bytes); + if (mem != NULL) memset(mem, 0, bytes); return mem; } #endif /* DEFINE_CALLOC */ diff --git a/newlib/libc/stdlib/reallocarray.c b/newlib/libc/stdlib/reallocarray.c index 4b6cccb70..d1f63c66b 100644 --- a/newlib/libc/stdlib/reallocarray.c +++ b/newlib/libc/stdlib/reallocarray.c @@ -16,27 +16,20 @@ */ #include -__FBSDID("$FreeBSD: head/lib/libc/stdlib/reallocarray.c 282314 2015-05-01 18:32:16Z bapt $"); - #include #include #include #include -/* - * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX - * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW - */ -#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) - void * reallocarray(void *optr, size_t nmemb, size_t size) { + ptrdiff_t bytes; - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && - nmemb > 0 && SIZE_MAX / nmemb < size) { + if (__builtin_mul_overflow (nmemb, size, &bytes)) + { errno = ENOMEM; - return (NULL); + return NULL; } - return (realloc(optr, size * nmemb)); + return realloc(optr, bytes); } -- 2.28.0