[PATCH] Fix two memory leaks in findtextrel and strip.

Mark Wielaard mjw at redhat.com
Fri Mar 1 11:02:43 UTC 2013


The findtextrel leak is real, when processing lots of files without text
relocations the files and memory wouldn't be released. The strip leak is
minor since it can only happen when using -f which only allows one file
as argument. But it is nice to see valgrind say:

  "All heap blocks were freed -- no leaks are possible".

Signed-off-by: Mark Wielaard <mjw at redhat.com>
---
 src/ChangeLog     |    7 +++++++
 src/findtextrel.c |    2 +-
 src/strip.c       |    7 ++++++-
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 6e219e6..2275c5c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2013-03-01  Mark Wielaard  <mjw at redhat.com>
+
+	* findtextrel.c (process_file): Release ELF and close file when not
+	text relocations are found.
+	* strip.c (handle_elf): Track memory used for .debuglink section data
+	and free when done.
+
 2013-02-24  Mark Wielaard  <mjw at redhat.com>
 
 	* elflint.c (check_symtab): Add __bss_start__ to the list of symbols
diff --git a/src/findtextrel.c b/src/findtextrel.c
index 39f7c92..9fd8163 100644
--- a/src/findtextrel.c
+++ b/src/findtextrel.c
@@ -311,7 +311,7 @@ process_file (const char *fname, bool more_than_one)
   if (!have_textrel)
     {
       error (0, 0, gettext ("no text relocations reported in '%s'"), fname);
-      return 1;
+      goto err_elf_close;
     }
 
   int fd2 = -1;
diff --git a/src/strip.c b/src/strip.c
index 5e9c883..05adc20 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -432,6 +432,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
   Elf_Data debuglink_crc_data;
   bool any_symtab_changes = false;
   Elf_Data *shstrtab_data = NULL;
+  void *debuglink_buf = NULL;
 
   /* Create the full name of the file.  */
   if (prefix != NULL)
@@ -1046,7 +1047,8 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
       shdr_info[cnt].data->d_align = 4;
       shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size
 	= crc_offset + 4;
-      shdr_info[cnt].data->d_buf = xcalloc (1, shdr_info[cnt].data->d_size);
+      debuglink_buf = xcalloc (1, shdr_info[cnt].data->d_size);
+      shdr_info[cnt].data->d_buf = debuglink_buf;
 
       strcpy (shdr_info[cnt].data->d_buf, debug_basename);
 
@@ -2013,6 +2015,9 @@ while computing checksum for debug information"));
 	      free (shdr_info[cnt].debug_data->d_buf);
 	  }
 
+      /* Free data we allocated for the .gnu_debuglink section. */
+      free (debuglink_buf);
+
       /* Free the memory.  */
       if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
 	free (shdr_info);
-- 
1.7.1



More information about the elfutils-devel mailing list