This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 04/12] fix a couple of bfd leaks


While reading all the code that opens and closes BFDs in GDB,
I found a few places where BFDs are leaked.  This fixes some of those.

I think there is one more leak in the Darwin code that I didn't fix.

	* remote-mips.c (mips_load_srec): Use make_cleanup_bfd_close.
	(pmon_load_fast): Likewise.
	* m32r-rom.c (m32r_load): Use make_cleanup_bfd_close.
	(m32r_upload_command): Likewise.
	* dsrec.c (load_srec): Use make_cleanup_bfd_close.
	* solib-darwin.c (darwin_solib_get_all_image_info_addr_at_init):
	Use make_cleanup_bfd_close.
---
 gdb/dsrec.c        |    4 ++++
 gdb/m32r-rom.c     |    6 ++++++
 gdb/remote-mips.c  |    8 ++++++++
 gdb/solib-darwin.c |   19 +++++++++++++------
 4 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/gdb/dsrec.c b/gdb/dsrec.c
index 54f2971..8eefe1e 100644
--- a/gdb/dsrec.c
+++ b/gdb/dsrec.c
@@ -57,6 +57,7 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
   int reclen;
   time_t start_time, end_time;
   unsigned long data_count = 0;
+  struct cleanup *cleanup;
 
   srec = (char *) alloca (maxrecsize + 1);
 
@@ -67,9 +68,11 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
       return;
     }
 
+  cleanup = make_cleanup_bfd_close (abfd);
   if (bfd_check_format (abfd, bfd_object) == 0)
     {
       printf_filtered (_("File is not an object file\n"));
+      do_cleanups (cleanup);
       return;
     }
 
@@ -171,6 +174,7 @@ load_srec (struct serial *desc, const char *file, bfd_vma load_offset,
   serial_flush_input (desc);
 
   report_transfer_performance (data_count, start_time, end_time);
+  do_cleanups (cleanup);
 }
 
 /*
diff --git a/gdb/m32r-rom.c b/gdb/m32r-rom.c
index 92a73d0..9dbcd26 100644
--- a/gdb/m32r-rom.c
+++ b/gdb/m32r-rom.c
@@ -125,6 +125,7 @@ m32r_load (char *filename, int from_tty)
   bfd *abfd;
   unsigned int data_count = 0;
   struct timeval start_time, end_time;
+  struct cleanup *cleanup;
 
   if (filename == NULL || filename[0] == 0)
     filename = get_exec_file (1);
@@ -132,6 +133,7 @@ m32r_load (char *filename, int from_tty)
   abfd = gdb_bfd_ref (bfd_openr (filename, 0));
   if (!abfd)
     error (_("Unable to open file %s."), filename);
+  cleanup = make_cleanup_bfd_close (abfd);
   if (bfd_check_format (abfd, bfd_object) == 0)
     error (_("File is not an object file."));
   gettimeofday (&start_time, NULL);
@@ -189,6 +191,7 @@ m32r_load (char *filename, int from_tty)
      confused...  */
 
   clear_symtab_users (0);
+  do_cleanups (cleanup);
 }
 
 static void
@@ -435,6 +438,7 @@ m32r_upload_command (char *args, int from_tty)
   char buf[1024];
   struct hostent *hostent;
   struct in_addr inet_addr;
+  struct cleanup *cleanup;
 
   /* First check to see if there's an ethernet port!  */
   monitor_printf ("ust\r");
@@ -526,6 +530,7 @@ m32r_upload_command (char *args, int from_tty)
 
   gettimeofday (&end_time, NULL);
   abfd = gdb_bfd_ref (bfd_openr (args, 0));
+  cleanup = make_cleanup_bfd_close (abfd);
   if (abfd != NULL)
     {		/* Download is done -- print section statistics.  */
       if (bfd_check_format (abfd, bfd_object) == 0)
@@ -566,6 +571,7 @@ m32r_upload_command (char *args, int from_tty)
      confused...  */
 
   clear_symtab_users (0);
+  do_cleanups (cleanup);
 }
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */
diff --git a/gdb/remote-mips.c b/gdb/remote-mips.c
index 30fd948..5020d2a 100644
--- a/gdb/remote-mips.c
+++ b/gdb/remote-mips.c
@@ -2784,6 +2784,7 @@ mips_load_srec (char *args)
   unsigned int i;
   unsigned int srec_frame = 200;
   int reclen;
+  struct cleanup *cleanup;
   static int hashmark = 1;
 
   buffer = alloca (srec_frame * 2 + 256);
@@ -2795,9 +2796,11 @@ mips_load_srec (char *args)
       return;
     }
 
+  cleanup = make_cleanup_bfd_close (abfd);
   if (bfd_check_format (abfd, bfd_object) == 0)
     {
       printf_filtered ("File is not an object file\n");
+      do_cleanups (cleanup);
       return;
     }
 
@@ -2851,6 +2854,7 @@ mips_load_srec (char *args)
   send_srec (srec, reclen, abfd->start_address);
 
   serial_flush_input (mips_desc);
+  do_cleanups (cleanup);
 }
 
 /*
@@ -3367,6 +3371,7 @@ pmon_load_fast (char *file)
   int bintotal = 0;
   int final = 0;
   int finished = 0;
+  struct cleanup *cleanup;
 
   buffer = (char *) xmalloc (MAXRECSIZE + 1);
   binbuf = (unsigned char *) xmalloc (BINCHUNK);
@@ -3377,10 +3382,12 @@ pmon_load_fast (char *file)
       printf_filtered ("Unable to open file %s\n", file);
       return;
     }
+  cleanup = make_cleanup_bfd_close (abfd);
 
   if (bfd_check_format (abfd, bfd_object) == 0)
     {
       printf_filtered ("File is not an object file\n");
+      do_cleanups (cleanup);
       return;
     }
 
@@ -3504,6 +3511,7 @@ pmon_load_fast (char *file)
       pmon_end_download (final, bintotal);
     }
 
+  do_cleanups (cleanup);
   return;
 }
 
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 242f8cc..8d8da7e 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -357,6 +357,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
   gdb_byte *interp_name;
   CORE_ADDR load_addr = 0;
   bfd *dyld_bfd = NULL;
+  struct cleanup *cleanup;
 
   /* This method doesn't work with an attached process.  */
   if (current_inferior ()->attach_flag)
@@ -367,24 +368,30 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
   if (!interp_name)
     return;
 
+  cleanup = make_cleanup (null_cleanup, NULL);
+
   /* Create a bfd for the interpreter.  */
   dyld_bfd = gdb_bfd_ref (bfd_openr (interp_name, gnutarget));
   if (dyld_bfd)
     {
       bfd *sub;
 
+      make_cleanup_bfd_close (dyld_bfd);
       sub = bfd_mach_o_fat_extract (dyld_bfd, bfd_object,
 				    gdbarch_bfd_arch_info (target_gdbarch));
       if (sub)
-	dyld_bfd = sub;
-      else
 	{
-	  gdb_bfd_unref (dyld_bfd);
-	  dyld_bfd = NULL;
+	  dyld_bfd = gdb_bfd_ref (sub);
+	  make_cleanup_bfd_close (sub);
 	}
+      else
+	dyld_bfd = NULL;
     }
   if (!dyld_bfd)
-    return;
+    {
+      do_cleanups (cleanup);
+      return;
+    }
 
   /* We find the dynamic linker's base address by examining
      the current pc (which should point at the entry point for the
@@ -396,7 +403,7 @@ darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
   info->all_image_addr =
     lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");
 
-  gdb_bfd_unref (dyld_bfd);
+  do_cleanups (cleanup);
 
   if (info->all_image_addr == 0)
     return;
-- 
1.7.7.6


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]