This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: Memory leak in dwarf2.c
- From: Alan Modra <amodra at gmail dot com>
- To: binutils at sourceware dot org
- Date: Mon, 2 Sep 2019 17:09:37 +0930
- Subject: Re: Memory leak in dwarf2.c
- References: <1931e130-0fe6-98a7-8c23-993a7ec30879@gmx.net> <20190902013320.GA30165@bubble.grove.modra.org>
On Mon, Sep 02, 2019 at 11:03:20AM +0930, Alan Modra wrote:
> This fixes a small leak of debug_filename. bfd_openr copies the file
> name since git commit 1be5090bca.
That wasn't the best patch I ever committed. There a more places that
need attention in the same file.
PR 11983
* dwarf2.c (read_alt_indirect_string): Free debug_filename
on success. Tidy.
(read_alt_indirect_ref): Likewise.
diff --git a/bfd/dwarf2.c b/bfd/dwarf2.c
index 88638b903f..ed6dcd48c7 100644
--- a/bfd/dwarf2.c
+++ b/bfd/dwarf2.c
@@ -802,20 +802,21 @@ read_alt_indirect_string (struct comp_unit * unit,
if (stash->alt_bfd_ptr == NULL)
{
- bfd * debug_bfd;
- char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
+ bfd *debug_bfd;
+ char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
if (debug_filename == NULL)
return NULL;
- if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
- || ! bfd_check_format (debug_bfd, bfd_object))
- {
- if (debug_bfd)
- bfd_close (debug_bfd);
+ debug_bfd = bfd_openr (debug_filename, NULL);
+ free (debug_filename);
+ if (debug_bfd == NULL)
+ /* FIXME: Should we report our failure to follow the debuglink ? */
+ return NULL;
- /* FIXME: Should we report our failure to follow the debuglink ? */
- free (debug_filename);
+ if (!bfd_check_format (debug_bfd, bfd_object))
+ {
+ bfd_close (debug_bfd);
return NULL;
}
stash->alt_bfd_ptr = debug_bfd;
@@ -850,20 +851,21 @@ read_alt_indirect_ref (struct comp_unit * unit,
if (stash->alt_bfd_ptr == NULL)
{
- bfd * debug_bfd;
- char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
+ bfd *debug_bfd;
+ char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
if (debug_filename == NULL)
return FALSE;
- if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
- || ! bfd_check_format (debug_bfd, bfd_object))
- {
- if (debug_bfd)
- bfd_close (debug_bfd);
+ debug_bfd = bfd_openr (debug_filename, NULL);
+ free (debug_filename);
+ if (debug_bfd == NULL)
+ /* FIXME: Should we report our failure to follow the debuglink ? */
+ return NULL;
- /* FIXME: Should we report our failure to follow the debuglink ? */
- free (debug_filename);
+ if (!bfd_check_format (debug_bfd, bfd_object))
+ {
+ bfd_close (debug_bfd);
return NULL;
}
stash->alt_bfd_ptr = debug_bfd;
--
Alan Modra
Australia Development Lab, IBM