changeset 14468:393c747ad672

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>.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 24 Mar 2011 13:10:38 -0700
parents d8dcb84dcdf0
children a9eb5e05648e
files ChangeLog lib/xmalloc.c
diffstat 2 files changed, 16 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -52,8 +52,16 @@
 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;
 }