]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
Fix two memory leaks in findtextrel and strip.
authorMark Wielaard <mjw@redhat.com>
Fri, 1 Mar 2013 10:51:58 +0000 (11:51 +0100)
committerMark Wielaard <mjw@redhat.com>
Fri, 1 Mar 2013 10:51:58 +0000 (11:51 +0100)
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@redhat.com>
src/ChangeLog
src/findtextrel.c
src/strip.c

index 6e219e613aa047ece733c3a4c8d814c32d2408a8..2275c5c2feaf62fe20a842082c6e1fee10eef5ad 100644 (file)
@@ -1,3 +1,10 @@
+2013-03-01  Mark Wielaard  <mjw@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@redhat.com>
 
        * elflint.c (check_symtab): Add __bss_start__ to the list of symbols
index 39f7c924f39333578030272b469a230eb074ecaa..9fd81633b41252e964210d5d69aae65ce05c63c5 100644 (file)
@@ -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;
index 5e9c88338b1f44e997734b5a620b32e6c9fc6769..05adc206598c03659c18c0814da6fe7abf7676b8 100644 (file)
@@ -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);