]> sourceware.org Git - newlib-cygwin.git/commitdiff
libc/stdlib: Use __builtin_mul_overflow for reallocarray and calloc
authorKeith Packard via Newlib <newlib@sourceware.org>
Tue, 11 Aug 2020 23:05:40 +0000 (16:05 -0700)
committerCorinna Vinschen <corinna@vinschen.de>
Wed, 12 Aug 2020 08:09:56 +0000 (10:09 +0200)
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 <keithp@keithp.com>
newlib/libc/stdlib/mallocr.c
newlib/libc/stdlib/nano-mallocr.c
newlib/libc/stdlib/reallocarray.c

index 26d1c89ccb79f386c3ce44487adc9faa30a5894c..9ad720adafc9dbe8d3f19cab80df01e4b014dab6 100644 (file)
@@ -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;
index 13b72c99ffd7007b53c2e3270a56da237857742a..04465eb9e5e5f1c12ab418d94534251e3cfe126e 100644 (file)
@@ -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 */
index 4b6cccb7040a30060564e13b66da23337696747a..d1f63c66b30ef4986ac2e2d29f41e8d5561249cd 100644 (file)
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: head/lib/libc/stdlib/reallocarray.c 282314 2015-05-01 18:32:16Z bapt $");
-
 #include <sys/types.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdlib.h>
 
-/*
- * 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);
 }
This page took 0.038682 seconds and 5 git commands to generate.