]> Savannah Git Hosting - gnulib.git/commitdiff
reallocarray: simplify
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 25 Oct 2024 04:34:12 +0000 (21:34 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 25 Oct 2024 04:57:18 +0000 (21:57 -0700)
* lib/reallocarray.c (reallocarray): Use simpler workaround
for realloc glitch, which does not involve malloc.
* modules/reallocarray (Depends-on): Remove malloc-posix.

ChangeLog
lib/reallocarray.c
modules/reallocarray

index 0a74a6483c83e28c41d44aaabb6e68eb3f298ecc..9226a54c1866e2157aa63014f9a31664511d9e7e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-10-24  Paul Eggert  <eggert@cs.ucla.edu>
+
+       reallocarray: simplify
+       * lib/reallocarray.c (reallocarray): Use simpler workaround
+       for realloc glitch, which does not involve malloc.
+       * modules/reallocarray (Depends-on): Remove malloc-posix.
+
 2024-10-23  Paul Eggert  <eggert@cs.ucla.edu>
 
        ialloc: fix realloc-gnu dependency
index e0377d087075b43f2aabd5e9566af38c0163972c..bae88c937de52ff7e98682139fcac7f6c4f0b25d 100644 (file)
@@ -33,17 +33,11 @@ reallocarray (void *ptr, size_t nmemb, size_t size)
       return NULL;
     }
 
-  /* Avoid calling realloc (ptr, 0), since that is undefined behaviour in
-     ISO C 23 and since the GNU libc behaviour may possibly change.  */
+  /* Work around realloc glitch by treating a 0 size as if it were 1,
+     to avoid undefined behavior in strict C23 platforms,
+     and so that returning NULL is equivalent to failing.  */
   if (nbytes == 0)
-    {
-      void *new_ptr = malloc (1);
-      if (new_ptr == NULL)
-        /* errno is set here.  */
-        return NULL;
-      free (ptr);
-      return new_ptr;
-    }
+    nbytes = 1;
 
   /* Call realloc, setting errno to ENOMEM on failure.  */
   return realloc (ptr, nbytes);
index dace5e636be2eded894a14167d07e9654b223567..41c80e454b8b83b5563bf078410affcc9e2779be 100644 (file)
@@ -8,7 +8,6 @@ m4/reallocarray.m4
 
 Depends-on:
 extensions
-malloc-posix   [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
 realloc-posix  [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
 stdckdint      [test $HAVE_REALLOCARRAY = 0 || test $REPLACE_REALLOCARRAY = 1]
 stdlib