PR23560, PR23561, readelf memory leaks

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

PR23560, PR23561, readelf memory leaks

Alan Modra-3
        PR 23560
        PR 23561
        * dwarf.c (display_debug_frames): Move fde_fc earlier.  Free
        fde_fc col_type and col_offset.
        * readelf.c (apply_relocations): Move symsec check earlier.
        (free_debug_section): Free reloc_info.
        (process_notes_at): Free pnotes on error path.
        (process_object): Free dump_sects here..
        (process_archive): ..not here.

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index 9c96f471f8..7ca8b78ccc 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -7801,6 +7801,7 @@ display_debug_frames (struct dwarf_section *section,
       unsigned int offset_size;
       unsigned int initial_length_size;
       bfd_boolean all_nops;
+      static Frame_Chunk fde_fc;
 
       saved_start = start;
 
@@ -7898,7 +7899,6 @@ display_debug_frames (struct dwarf_section *section,
       else
  {
   unsigned char *look_for;
-  static Frame_Chunk fde_fc;
   unsigned long segment_selector;
 
   if (is_eh)
@@ -8705,6 +8705,17 @@ display_debug_frames (struct dwarf_section *section,
       if (do_debug_frames_interp && ! all_nops)
  frame_display_row (fc, &need_col_headers, &max_regs);
 
+      if (fde_fc.col_type != NULL)
+ {
+  free (fde_fc.col_type);
+  fde_fc.col_type = NULL;
+ }
+      if (fde_fc.col_offset != NULL)
+ {
+  free (fde_fc.col_offset);
+  fde_fc.col_offset = NULL;
+ }
+
       start = block_end;
       eh_addr_size = saved_eh_addr_size;
     }
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 4326cd00ca..90f33bdd21 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -13333,6 +13333,11 @@ apply_relocations (Filedata *                 filedata,
   || relsec->sh_link >= filedata->file_header.e_shnum)
  continue;
 
+      symsec = filedata->section_headers + relsec->sh_link;
+      if (symsec->sh_type != SHT_SYMTAB
+  && symsec->sh_type != SHT_DYNSYM)
+ return FALSE;
+
       is_rela = relsec->sh_type == SHT_RELA;
 
       if (is_rela)
@@ -13352,10 +13357,6 @@ apply_relocations (Filedata *                 filedata,
       if (filedata->file_header.e_machine == EM_SH)
  is_rela = FALSE;
 
-      symsec = filedata->section_headers + relsec->sh_link;
-      if (symsec->sh_type != SHT_SYMTAB
-  && symsec->sh_type != SHT_DYNSYM)
- return FALSE;
       symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
 
       for (rp = relocs; rp < relocs + num_relocs; ++rp)
@@ -14395,6 +14396,13 @@ free_debug_section (enum dwarf_section_display_enum debug)
   section->start = NULL;
   section->address = 0;
   section->size = 0;
+
+  if (section->reloc_info != NULL)
+    {
+      free (section->reloc_info);
+      section->reloc_info = NULL;
+      section->num_relocs = 0;
+    }
 }
 
 static bfd_boolean
@@ -19341,6 +19349,7 @@ process_notes_at (Filedata *           filedata,
     {
       warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
     (long) align);
+      free (pnotes);
       return FALSE;
     }
 
@@ -20011,6 +20020,13 @@ process_object (Filedata * filedata)
   filedata->string_table = NULL;
   filedata->string_table_length = 0;
 
+  if (filedata->dump_sects != NULL)
+    {
+      free (filedata->dump_sects);
+      filedata->dump_sects = NULL;
+      filedata->num_dump_sects = 0;
+    }
+
   if (dynamic_strings)
     {
       free (dynamic_strings);
@@ -20316,13 +20332,6 @@ process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
     ret = FALSE;
         }
 
-      if (filedata->dump_sects != NULL)
- {
-  free (filedata->dump_sects);
-  filedata->dump_sects = NULL;
-  filedata->num_dump_sects = 0;
- }
-
       free (qualified_name);
     }
 

--
Alan Modra
Australia Development Lab, IBM