[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 11:47:02 -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 10:17 AM, Eric Blake wrote:
> How should gnulib react?  And are there any existing GNU programs that
> would break if C99 realloc semantics were enforced?

My kneejerk reaction is that
programs should be written so as to be portable to
both C89 (which GNU implements) and C99 realloc.
Programs that rely on GNU behavior but get C99 behavior
will have memory leaks, but that's better than crashing.
GNU programs should not be written to rely on C99 behavior,
because they may crash.

I don't offhand know of any existing GNU code that expects C99
behavior and would crash with GNU behavior.

The issue is well worth documenting in the gnulib manual.

It should be fairly easy to write code that is portable
to both C89 and C99 and does not leak, by using free (p) rather
than realloc (p, 0).  For example, we could apply the following
patch to xmalloc.c:

--- a/lib/xmalloc.c                                                             
+++ b/lib/xmalloc.c                                                             
@@ -52,10 +52,18 @@ xmalloc (size_t n)
 void *                                                                         
 xrealloc (void *p, size_t n)                                                   
-  p = realloc (p, n);                                                          
-  if (!p && n != 0)                                                            
-    xalloc_die ();                                                             
-  return p;                                                                    
+  if (n)                                                                       
+    {                                                                          
+      p = realloc (p, n);                                                      
+      if (!p)                                                                  
+        xalloc_die ();                                                         
+      return p;                                                                
+    }                                                                          
+  else                                                                         
+    {                                                                          
+      free (p);                                                                
+      return NULL;                                                             
+    }                                                                          
 /* If P is null, allocate a block of at least *PN bytes; otherwise,        

reply via email to

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