This is the mail archive of the
binutils@sourceware.cygnus.com
mailing list for the binutils project.
Re: Patch to fix memory leak in bfd
- To: ian@zembu.com
- Subject: Re: Patch to fix memory leak in bfd
- From: Nick Clifton <nickc@cygnus.com>
- Date: Mon, 6 Sep 1999 09:57:21 +0100
- CC: binutils@sourceware.cygnus.com
- Reply-to: nickc@cygnus.co.uk
Hi Ian,
: Thanks. I wouldn't call that a memory leak--a memory leak is a case
: where memory is allocated but never freed. What you fixed is a case
: of memory corruption.
True.
: Incidentally, that code appears to have another, less serious, bug,
: which is that if realloc or calloc fail, it should call bfd_set_error
: (bfd_error_no_memory). Since it doesn't, a memory allocation failure
: will not be reported back to the user correctly. Most code does this
: by calling bfd_malloc or bfd_realloc, in libbfd.c, rather than malloc
: or realloc.
Good point. I have checked in the patch below to correct this.
Cheers
Nick
1999-09-06 Nick Clifton <nickc@cygnus.com>
* elflink.h (elf_gc_record_vtentry): Use bfd_zmalloc and
bfd_realloc instead of calloc and realloc.
Index: elflink.h
===================================================================
RCS file: /cvs/binutils/binutils/bfd/elflink.h,v
retrieving revision 1.31
diff -p -w -r1.31 elflink.h
*** elflink.h 1999/09/04 16:26:08 1.31
--- elflink.h 1999/09/06 08:56:54
*************** elf_gc_record_vtentry (abfd, sec, h, add
*** 6423,6448 ****
if (ptr)
{
! size_t oldbytes;
! ptr = realloc (ptr-1, bytes);
! if (ptr == NULL)
! return false;
oldbytes = (h->vtable_entries_size/FILE_ALIGN + 1) * sizeof(boolean);
memset (((char *)ptr) + oldbytes, 0, bytes - oldbytes);
}
else
! {
! ptr = calloc (1, bytes);
if (ptr == NULL)
return false;
- }
/* And arrange for that done flag to be at index -1. */
h->vtable_entries_used = ptr+1;
h->vtable_entries_size = size;
}
h->vtable_entries_used[addend / FILE_ALIGN] = true;
return true;
--- 6423,6449 ----
if (ptr)
{
! ptr = bfd_realloc (ptr - 1, bytes);
! if (ptr != NULL)
! {
! size_t oldbytes;
oldbytes = (h->vtable_entries_size/FILE_ALIGN + 1) * sizeof (boolean);
memset (((char *)ptr) + oldbytes, 0, bytes - oldbytes);
}
+ }
else
! ptr = bfd_zmalloc (bytes);
!
if (ptr == NULL)
return false;
/* And arrange for that done flag to be at index -1. */
h->vtable_entries_used = ptr + 1;
h->vtable_entries_size = size;
}
+
h->vtable_entries_used[addend / FILE_ALIGN] = true;
return true;