diff options
author | Nick Clifton <nickc@redhat.com> | 2011-09-08 16:06:11 +0000 |
---|---|---|
committer | Nick Clifton <nickc@redhat.com> | 2011-09-08 16:06:11 +0000 |
commit | 103ae312ba3c0b864851454b34a2104eff3370ad (patch) | |
tree | b8fb22483fbb41a43ff05a110f76ced35d280715 /bfd | |
parent | 08711b9a36344df9e29be436d9f959324fff4f42 (diff) | |
download | gdb-103ae312ba3c0b864851454b34a2104eff3370ad.zip gdb-103ae312ba3c0b864851454b34a2104eff3370ad.tar.gz gdb-103ae312ba3c0b864851454b34a2104eff3370ad.tar.bz2 |
* bfdwin.c (bfd_get_file_window): Fix memory leak.
Diffstat (limited to 'bfd')
-rw-r--r-- | bfd/ChangeLog | 4 | ||||
-rw-r--r-- | bfd/bfdwin.c | 41 |
2 files changed, 29 insertions, 16 deletions
diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 523fe17..d3c38e7 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,7 @@ +2011-09-08 Bernd Jendrissek <bernd.jendrissek@gmail.com> + + * bfdwin.c (bfd_get_file_window): Fix memory leak. + 2011-09-07 Alan Modra <amodra@gmail.com> PR ld/13131 diff --git a/bfd/bfdwin.c b/bfd/bfdwin.c index 63ad5ed..4103e9c 100644 --- a/bfd/bfdwin.c +++ b/bfd/bfdwin.c @@ -1,5 +1,5 @@ /* Support for memory-mapped windows into a BFD. - Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007, 2008, 2009 + Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2011 Free Software Foundation, Inc. Written by Cygnus Support. @@ -128,17 +128,16 @@ bfd_get_file_window (bfd *abfd, if (pagesize == 0) abort (); - if (i == 0) + if (i == NULL) { i = bfd_zmalloc (sizeof (bfd_window_internal)); - windowp->i = i; - if (i == 0) + if (i == NULL) return FALSE; - i->data = 0; + i->data = NULL; } #ifdef HAVE_MMAP if (ok_to_map - && (i->data == 0 || i->mapped == 1) + && (i->data == NULL || i->mapped == 1) && (abfd->flags & BFD_IN_MEMORY) == 0) { file_ptr file_offset, offset2; @@ -156,9 +155,9 @@ bfd_get_file_window (bfd *abfd, if (abfd->iostream == NULL && (abfd->iovec == NULL || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0)) - return FALSE; - fd = fileno ((FILE *) abfd->iostream); + goto free_and_fail; + fd = fileno ((FILE *) abfd->iostream); /* Compute offsets and size for mmap and for the user's data. */ offset2 = offset % pagesize; if (offset2 < 0) @@ -169,10 +168,10 @@ bfd_get_file_window (bfd *abfd, real_size -= real_size % pagesize; /* If we're re-using a memory region, make sure it's big enough. */ - if (i->data && i->size < size) + if (i->data != NULL && i->size < size) { munmap (i->data, i->size); - i->data = 0; + i->data = NULL; } i->data = mmap (i->data, real_size, writable ? PROT_WRITE | PROT_READ : PROT_READ, @@ -185,11 +184,10 @@ bfd_get_file_window (bfd *abfd, /* An error happened. Report it, or try using malloc, or something. */ bfd_set_error (bfd_error_system_call); - i->data = 0; windowp->data = 0; if (debug_windows) fprintf (stderr, "\t\tmmap failed!\n"); - return FALSE; + goto free_and_fail; } if (debug_windows) fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n", @@ -198,6 +196,8 @@ bfd_get_file_window (bfd *abfd, windowp->data = (bfd_byte *) i->data + offset2; windowp->size = size; i->mapped = 1; + i->refcount = 1; + windowp->i = i; return TRUE; } else if (debug_windows) @@ -228,15 +228,18 @@ bfd_get_file_window (bfd *abfd, if (i->data == NULL) { if (size_to_alloc == 0) - return TRUE; - return FALSE; + { + windowp->i = i; + return TRUE; + } + goto free_and_fail; } i->refcount = 1; if (bfd_seek (abfd, offset, SEEK_SET) != 0) - return FALSE; + goto free_and_fail; i->size = bfd_bread (i->data, size, abfd); if (i->size != size) - return FALSE; + goto free_and_fail; i->mapped = 0; #ifdef HAVE_MPROTECT if (!writable) @@ -249,7 +252,13 @@ bfd_get_file_window (bfd *abfd, #endif windowp->data = i->data; windowp->size = i->size; + windowp->i = i; return TRUE; + + free_and_fail: + /* We have a bfd_window_internal, but an error occurred. Free it. */ + free (i); + return FALSE; } #endif /* USE_MMAP */ |