]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Tidy leaked objcopy memory
authorAlan Modra <amodra@gmail.com>
Wed, 29 Mar 2023 11:35:59 +0000 (22:05 +1030)
committerAlan Modra <amodra@gmail.com>
Thu, 30 Mar 2023 04:48:02 +0000 (15:18 +1030)
* 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.

binutils/objcopy.c
binutils/wrstabs.c

index cf830442b3c1b6a0c2c43fcb16c08bd33170bfd5..72585ea9ed725baf4e599d5d14f5c4dd37004c58 100644 (file)
@@ -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,
index 2df2b0414343cd1279acded7d7928a26509a09c8..53ade2eafbdea471bd1715de4ec9a3de1b02b3ab 100644 (file)
@@ -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.  */