bug-binutils
[Top][All Lists]
Advanced

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

Re: [Bug binutils/868] New: bfd leaks memory in several places


From: Nick Clifton
Subject: Re: [Bug binutils/868] New: bfd leaks memory in several places
Date: Thu, 05 May 2005 15:34:51 +0100
User-agent: Mozilla Thunderbird 1.0 (X11/20041206)

Hi John,

bfd has several memory leaks. I'm fixing the ones in dwarf2.c, but a simple grep
for 'bfd_realloc' shows several obvious leaks on failure:

./bfd/bfdio.c:        bim->buffer = bfd_realloc (bim->buffer, newsize);
[etc]

The least intrusive way to resolve most of these would be to provide a new function called, say, bfd_realloc_or_free() which could used to replace most of the calls to bfd_realloc. It could be implemented like this:


Index: bfd/libbfd.c
===================================================================
RCS file: /cvs/src/src/bfd/libbfd.c,v
retrieving revision 1.38
diff -c -3 -p -r1.38 libbfd.c
*** bfd/libbfd.c        4 May 2005 15:53:33 -0000       1.38
--- bfd/libbfd.c        5 May 2005 14:32:21 -0000
*************** bfd_realloc (void *ptr, bfd_size_type si
*** 180,185 ****
--- 180,214 ----
    return ret;
  }

+
+ /* Reallocate memory using realloc.
+    If this fails the pointer is freed before returning. */
+
+ void *
+ bfd_realloc_or_free (void *ptr, bfd_size_type size)
+ {
+   size_t amount = (size_t) size;
+   void *ret;
+
+   if (size != amount)
+     ret = NULL;
+   else if (ptr == NULL)
+     ret = malloc (amount);
+   else
+     ret = realloc (ptr, amount);
+
+   if (ret == NULL)
+     {
+       if (amount > 0)
+       bfd_set_error (bfd_error_no_memory);
+
+       if (ptr != NULL)
+       free (ptr);
+     }
+
+   return ret;
+ }
+
  /* Allocate memory using malloc and clear it.  */

  void *


What do you think ?

Cheers
  Nick




reply via email to

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