[binutils-gdb] Re: Memory leak in dwarf2.c

Alan Modra amodra@sourceware.org
Mon Sep 2 07:40:00 GMT 2019


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=217d2eaa69c2a5d58cdfd11619c4f2e41c986826

commit 217d2eaa69c2a5d58cdfd11619c4f2e41c986826
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Sep 2 16:48:44 2019 +0930

    Re: Memory leak in dwarf2.c
    
    	PR 11983
    	* dwarf2.c (read_alt_indirect_string): Free debug_filename
    	on success.  Tidy.
    	(read_alt_indirect_ref): Likewise.

Diff:
---
 bfd/ChangeLog |  2 ++
 bfd/dwarf2.c  | 38 ++++++++++++++++++++------------------
 2 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 47f694a..f033af7 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -3,6 +3,8 @@
 	PR 11983
 	* dwarf2.c (_bfd_dwarf2_slurp_debug_info): Free debug_filename
 	on success.  Tidy.
+	(read_alt_indirect_string): Likewise.
+	(read_alt_indirect_ref): Likewise.
 
 2019-08-31  Jim Wilson  <jimw@sifive.com>
 
diff --git a/bfd/dwarf2.c b/bfd/dwarf2.c
index 88638b9..ed6dcd4 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;



More information about the Binutils-cvs mailing list