]> Savannah Git Hosting - gnulib.git/commitdiff
realloc: don’t require success for nongrowth
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 4 Nov 2024 05:44:15 +0000 (21:44 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 5 Nov 2024 05:40:18 +0000 (21:40 -0800)
* lib/realloc.c (rpl_realloc): Do not require realloc (p, 0) to
succeed, as apparently glibc realloc (p, n) can sometimes fail
even when the region would not grow.

ChangeLog
doc/posix-functions/realloc.texi
lib/realloc.c

index 5edadc4ea8eee9c8a9de0e38a6d03b5dffb82892..ccad8e84f6245cc4c76cbf8b200696c11f2dc297 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-11-04  Paul Eggert  <eggert@cs.ucla.edu>
 
+       realloc: don’t require success for nongrowth
+       * lib/realloc.c (rpl_realloc): Do not require realloc (p, 0) to
+       succeed, as apparently glibc realloc (p, n) can sometimes fail
+       even when the region would not grow.
+
        calloc, malloc: tune a bit
        This applies mostly to non-glibc platforms, or to 32-bit
        glibc before glibc 2.30 (2019).
index 4be63c767a3187a7527291b7e5788aab35216b56..3d8cd1705b18285368b1d2b7182779a2b60f6837 100644 (file)
@@ -89,8 +89,8 @@ Portability problems not fixed by Gnulib:
 @item
 When not growing an already-allocated region, i.e.,
 when @code{p} points to a region of size @code{psize} and @code{n <= psize},
-the standards allow @code{realloc (p, n)} to fail and return a null pointer.
-It is not known which, if any, implementations actually fail in this situation.
+@code{realloc (p, n)} can fail and return a null pointer:
+glibc 2.40 and probably other platforms.
 
 @item
 If @code{realloc (p, 0)} frees @code{p} and returns a null pointer,
index a94e23023206c8a71df5bb447c6a0c54cb27d5dd..d0ed2169bc16dd45bb9714c0513b14ba0c96b566 100644 (file)
@@ -65,18 +65,14 @@ rpl_realloc (void *p, size_t n)
          due either to C23 or to (a)'s semantics being messy.
          Act like (b), as that's easy, matches GNU, BSD and V7 malloc,
          matches BSD and V7 realloc, and requires no extra code at
-         caller sites.
-         Do not fail if P is nonnull, though, as it's natural for callers
-         to assume that realloc (P, 0) can fail only when P is null.  */
+         caller sites.  */
 
       void *result = realloc (p, 1);
-      if (result != NULL)
-        return result;
 #if !HAVE_MALLOC_POSIX
-      if (p == NULL)
+      if (result == NULL)
         errno = ENOMEM;
 #endif
-      return p;
+      return result;
     }
 
   ptrdiff_t signed_n;