public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* Tidy leaked objcopy memory
@ 2023-03-30  4:49 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2023-03-30  4:49 UTC (permalink / raw)
  To: binutils

	* objcopy.c (delete_symbol_htabs): Also free symbols.
	(write_debugging_info): Free strings and syms once written.
	* wrstabs.c (write_stabs_in_sections_debugging_info): memset
	entire info struct.  Free hash tables before returning.  Free
	syms on error return.

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index cf830442b3c..72585ea9ed7 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -1065,6 +1065,10 @@ delete_symbol_htabs (void)
   htab_delete (weaken_specific_htab);
   htab_delete (redefine_specific_htab);
   htab_delete (redefine_specific_reverse_htab);
+
+  free (isympp);
+  if (osympp != isympp)
+    free (osympp);
 }
 
 /* Add a symbol to strip_specific_list.  */
@@ -4656,6 +4660,7 @@ write_debugging_info (bfd *obfd, void *dhandle,
       bfd_size_type symsize, stringsize;
       asection *stabsec, *stabstrsec;
       flagword flags;
+      bool ret;
 
       if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
 						    &symsize, &strings,
@@ -4682,17 +4687,19 @@ write_debugging_info (bfd *obfd, void *dhandle,
 	 the next thing the caller is going to do is copy over the
 	 real sections.  We may someday have to split the contents
 	 setting out of this function.  */
+      ret = true;
       if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
 	  || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
 					 stringsize))
 	{
 	  bfd_nonfatal_message (NULL, obfd, NULL,
 				_("can't set debugging section contents"));
-	  free (strings);
-	  return false;
+	  ret = false;
 	}
 
-      return true;
+      free (strings);
+      free (syms);
+      return ret;
     }
 
   bfd_nonfatal_message (NULL, obfd, NULL,
diff --git a/binutils/wrstabs.c b/binutils/wrstabs.c
index 2df2b041434..53ade2eafbd 100644
--- a/binutils/wrstabs.c
+++ b/binutils/wrstabs.c
@@ -469,16 +469,18 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
   struct string_hash_entry *h;
   bfd_byte *p;
 
+  memset (&info, 0, sizeof info);
   info.abfd = abfd;
 
-  info.symbols_size = 0;
   info.symbols_alloc = 500;
   info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
 
-  info.strings = NULL;
-  info.last_string = NULL;
   /* Reserve 1 byte for a null byte.  */
   info.strings_size = 1;
+  info.type_index = 1;
+  info.so_offset = -1;
+  info.fun_offset = -1;
+  info.pending_lbrac = (bfd_vma) -1;
 
   if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
 			    sizeof (struct string_hash_entry))
@@ -487,38 +489,28 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
     {
       non_fatal ("bfd_hash_table_init_failed: %s",
 		 bfd_errmsg (bfd_get_error ()));
-      return false;
+      goto fail;
     }
 
-  info.type_stack = NULL;
-  info.type_index = 1;
-  memset (&info.type_cache, 0, sizeof info.type_cache);
-  info.so_offset = -1;
-  info.fun_offset = -1;
-  info.last_text_address = 0;
-  info.nesting = 0;
-  info.fnaddr = 0;
-  info.pending_lbrac = (bfd_vma) -1;
-
   /* The initial symbol holds the string size.  */
   if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
-    return false;
+    goto fail;
 
   /* Output an initial N_SO symbol.  */
   info.so_offset = info.symbols_size;
   if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
-    return false;
+    goto fail;
 
   if (! debug_write (dhandle, &stab_fns, (void *) &info))
-    return false;
+    goto fail;
 
   if (info.pending_lbrac != (bfd_vma) -1)
-    return false;
+    goto fail;
 
   /* Output a trailing N_SO.  */
   if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
 			   (const char *) NULL))
-    return false;
+    goto fail;
 
   /* Put the string size in the initial symbol.  */
   bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
@@ -537,7 +529,17 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
       p += strlen ((char *) p) + 1;
     }
 
+  bfd_hash_table_free (&info.typedef_hash.table);
+  bfd_hash_table_free (&info.strhash.table);
   return true;
+
+ fail:
+  if (info.typedef_hash.table.memory)
+    bfd_hash_table_free (&info.typedef_hash.table);
+  if (info.strhash.table.memory)
+    bfd_hash_table_free (&info.strhash.table);
+  free (info.symbols);
+  return false;
 }
 
 /* Start writing out information for a compilation unit.  */

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-30  4:49 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30  4:49 Tidy leaked objcopy memory Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).