]> Savannah Git Hosting - gnulib.git/commitdiff
safe-alloc: simplify via reallocarray
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 18 Apr 2021 23:53:11 +0000 (16:53 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 19 Apr 2021 03:59:52 +0000 (20:59 -0700)
* lib/safe-alloc.c: Do not include xalloc-oversized.h.
(safe_alloc_alloc_n, safe_alloc_realloc_n):
Use reallocarray to check for size or ptrdiff_t overflow.
* modules/reallocarray (License): Switch from LGPL to LGPLv2+, as
this is needed for safe-alloc and anyway is more appropriate for
this library function common with BSD.
* modules/safe-alloc (Depends-on): Depend on reallocarray
rather than xalloc-oversized.

ChangeLog
lib/safe-alloc.c
modules/reallocarray
modules/safe-alloc

index 1d2607a0b33a6deff7b43a9c76a100b02ef46939..76d7141279e8476919107420b97947b4f9e75e46 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2021-04-18  Paul Eggert  <eggert@cs.ucla.edu>
 
+       safe-alloc: simplify via reallocarray
+       * lib/safe-alloc.c: Do not include xalloc-oversized.h.
+       (safe_alloc_alloc_n, safe_alloc_realloc_n):
+       Use reallocarray to check for size or ptrdiff_t overflow.
+       * modules/reallocarray (License): Switch from LGPL to LGPLv2+, as
+       this is needed for safe-alloc and anyway is more appropriate for
+       this library function common with BSD.
+       * modules/safe-alloc (Depends-on): Depend on reallocarray
+       rather than xalloc-oversized.
+
        xalloc-oversized: fix SIZE_MAX optimization bug
        * lib/xalloc-oversized.h (xalloc_count_t): Remove; no longer
        needed and was evidently error-prone anyway.
index 45e770015671c589046d468fdedb393b70f08176..116ac4371c29aa1a2e55010445ff9dd5c37c0af4 100644 (file)
@@ -22,8 +22,6 @@
 /* Specification.  */
 #include "safe-alloc.h"
 
-#include "xalloc-oversized.h"
-
 #include <stdlib.h>
 #include <stddef.h>
 #include <errno.h>
@@ -51,16 +49,10 @@ safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
       return 0;
     }
 
-  if (xalloc_oversized (count, size))
-    {
-      errno = ENOMEM;
-      return -1;
-    }
-
   if (zeroed)
     *(void **) ptrptr = calloc (count, size);
   else
-    *(void **) ptrptr = malloc (count * size);
+    *(void **) ptrptr = reallocarray (NULL, count, size);
 
   if (*(void **) ptrptr == NULL)
     return -1;
@@ -91,12 +83,7 @@ safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
       *(void **) ptrptr = NULL;
       return 0;
     }
-  if (xalloc_oversized (count, size))
-    {
-      errno = ENOMEM;
-      return -1;
-    }
-  tmp = realloc (*(void **) ptrptr, size * count);
+  tmp = reallocarray (*(void **) ptrptr, size, count);
   if (!tmp)
     return -1;
   *(void **) ptrptr = tmp;
index 11d32bb5fd13b6a8c4740ca5e9ecdb960e9567c1..a64c6fd2d0d015eb5e190e7e66f8e6d49ebcd34a 100644 (file)
@@ -26,7 +26,7 @@ Include:
 <stdlib.h>
 
 License:
-LGPL
+LGPLv2+
 
 Maintainer:
 all
index 13e78dec7b247d4854d9c005744a25220a4cfa26..9453b49ee58859e82fd6009a533adaf13b226588 100644 (file)
@@ -7,7 +7,7 @@ lib/safe-alloc.c
 m4/safe-alloc.m4
 
 Depends-on:
-xalloc-oversized
+reallocarray
 
 configure.ac:
 gl_SAFE_ALLOC