bug-gnulib
[Top][All Lists]
Advanced

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

Re: [Bug-gnulib] GNUlib realloc POSIX comformance


From: Paul Eggert
Subject: Re: [Bug-gnulib] GNUlib realloc POSIX comformance
Date: Wed, 17 Nov 2004 14:51:04 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Bruno Haible <address@hidden> writes:

> I don't see a POSIX violation here.

There's not a POSIX violation, but there is a memory leak in one very
unlikely circumstance.  If realloc (ptr, 1) fails due to memory
exhastion, it returns NULL without freeing ptr.  Hence under those
circumstances rpl_realloc (ptr, 0) will return NULL without freeing ptr.

I installed the following patch.

2004-11-17  Paul Eggert  <address@hidden>

        * lib/realloc.c (rpl_realloc): Call 'free' if n==0, since realloc
        might fail.  Problem reported by Yoann Vandoorselaere.

--- lib/realloc.c       9 Sep 2003 21:56:21 -0000       1.9
+++ lib/realloc.c       17 Nov 2004 22:47:27 -0000
@@ -1,5 +1,5 @@
-/* Work around bug on some systems where realloc (NULL, 0) fails.
-   Copyright (C) 1997, 2003 Free Software Foundation, Inc.
+/* realloc() function that is glibc compatible.
+   Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -32,8 +32,15 @@ void *
 rpl_realloc (void *p, size_t n)
 {
   if (n == 0)
-    n = 1;
-  if (p == 0)
+    {
+      n = 1;
+
+      /* In theory realloc might fail, so don't rely on it to free.  */
+      free (p);
+      p = NULL;
+    }
+
+  if (p == NULL)
     return malloc (n);
   return realloc (p, n);
 }




reply via email to

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