From: Mark Wielaard Date: Fri, 1 Mar 2013 10:51:58 +0000 (+0100) Subject: Fix two memory leaks in findtextrel and strip. X-Git-Tag: elfutils-0.156~37 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a58951b183fb3e051870b19c95d8082a3efa3ddb;p=thirdparty%2Felfutils.git Fix two memory leaks in findtextrel and strip. 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 --- diff --git a/src/ChangeLog b/src/ChangeLog index 6e219e613..2275c5c2f 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2013-03-01 Mark Wielaard + + * 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 * elflint.c (check_symtab): Add __bss_start__ to the list of symbols diff --git a/src/findtextrel.c b/src/findtextrel.c index 39f7c924f..9fd81633b 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 5e9c88338..05adc2065 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);