[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proper realloc(p,0) behavior?

From: Paul Eggert
Subject: Re: proper realloc(p,0) behavior?
Date: Thu, 24 Mar 2011 13:15:54 -0700
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20110307 Fedora/3.1.9-0.39.b3pre.fc14 Thunderbird/3.1.9

On 03/24/2011 12:47 PM, Eric Blake wrote:
> No argument by me if we enforce saner semantics to xrealloc.

No matter what we do it's problematic, but it's
a bit saner if xrealloc's callers can assume GNU behavior,
so I pushed this:

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
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 74a8614..4589e7d 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -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;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]