]> Savannah Git Hosting - gnulib.git/commitdiff
realloc-posix: Set errno when failing (regression 2029-10-29).
authorBruno Haible <bruno@clisp.org>
Thu, 31 Oct 2024 19:53:17 +0000 (20:53 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 31 Oct 2024 19:53:54 +0000 (20:53 +0100)
* lib/realloc.c (rpl_realloc): When failing, with HAVE_MALLOC_POSIX not
defined, set errno.

ChangeLog
lib/realloc.c

index 711d08f05831f515d894bca0f2587ae95a4cc815..b80e728c2ad2f668b117d0af6be8bda623861e63 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-10-31  Bruno Haible  <bruno@clisp.org>
+
+       realloc-posix: Set errno when failing (regression 2029-10-29).
+       * lib/realloc.c (rpl_realloc): When failing, with HAVE_MALLOC_POSIX not
+       defined, set errno.
+
 2024-10-31  Bruno Haible  <bruno@clisp.org>
 
        crc: Avoid potential conflict with other configure.ac files.
index b8570f39291d3f1c5252319354f16353d4c75b57..a94e23023206c8a71df5bb447c6a0c54cb27d5dd 100644 (file)
@@ -64,12 +64,19 @@ rpl_realloc (void *p, size_t n)
          Quite possibly future versions of POSIX will change,
          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 is convenient for Gnulib.
+         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.  */
 
       void *result = realloc (p, 1);
-      return result ? result : p;
+      if (result != NULL)
+        return result;
+#if !HAVE_MALLOC_POSIX
+      if (p == NULL)
+        errno = ENOMEM;
+#endif
+      return p;
     }
 
   ptrdiff_t signed_n;