xmalloc: Do not leak if underlying realloc is C99 compatible.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 24 Mar 2011 20:10:38 +0000 (13:10 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 24 Mar 2011 20:11:11 +0000 (13:11 -0700)
* lib/xmalloc.c (xrealloc): If N is zero, call 'free' directly.
This avoids a leak on C99-based systems.  See
<http://lists.gnu.org/archive/html/bug-gnulib/2011-03/msg00243.html>.

ChangeLog
lib/xmalloc.c

index 58f8195..c58e13c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-03-24  Paul Eggert  <eggert@cs.ucla.edu>
+
+       xmalloc: Do not leak if underlying realloc is C99 compatible.
+       * lib/xmalloc.c (xrealloc): If N is zero, call 'free' directly.
+       This avoids a leak on C99-based systems.  See
+       <http://lists.gnu.org/archive/html/bug-gnulib/2011-03/msg00243.html>.
+
 2011-03-24  Eric Blake  <eblake@redhat.com>
 
        realloc: document portability problem
index 74a8614..4589e7d 100644 (file)
@@ -52,8 +52,16 @@ xmalloc (size_t n)
 void *
 xrealloc (void *p, size_t n)
 {
+  if (!n)
+    {
+      /* The GNU and C99 realloc behaviors disagree here.  Act like
+         GNU, even if the underlying realloc is C99.  */
+      free (p);
+      return NULL;
+    }
+
   p = realloc (p, n);
-  if (!p && n != 0)
+  if (!p)
     xalloc_die ();
   return p;
 }