PR23560, PR23561, readelf memory leaks

Message ID 20200113121150.GR4433@bubble.grove.modra.org
State New
Headers show
Series
  • PR23560, PR23561, readelf memory leaks
Related show

Commit Message

Alan Modra Jan. 13, 2020, 12:11 p.m.
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.


-- 
Alan Modra
Australia Development Lab, IBM

Patch

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);
     }