]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
nm: Fix latent memory leak in show_symbols.
authorMark Wielaard <mark@klomp.org>
Thu, 29 Aug 2019 15:46:52 +0000 (17:46 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 29 Aug 2019 15:46:52 +0000 (17:46 +0200)
If there are just a handful of symbols then memory for them is
allocated on the stack, otherwise the memory is malloced. So before
freeing the memory we need to check the number of entries to know if
the memory was heap allocated or not. But since not all entries might
be used we might have decreased the number of entries to the number
we will actually show. Remember the original symbol entries to not
have a memory leak.

Signed-off-by: Mark Wielaard <mark@klomp.org>
src/ChangeLog
src/nm.c

index aeb623281a8c4277c85905e8c5f257a0fd7f60a2..cb64f7d91efcbfa1d1c0fbeff8e5f2946b8c8e21 100644 (file)
@@ -1,3 +1,8 @@
+2019-08-26  Mark Wielaard  <mark@klomp.org>
+
+       * nm.c (show_symbols): Remember nentries_orig and check before
+       freeing sym_mem.
+
 2019-07-05  Omar Sandoval  <osandov@fb.com>
 
        * Makefile.am: Remove -ldl.
index da1350b4c7a24e784d4b519bbe4fc46fced6df87..7f6cf2a226c42440eb3e573eba2c65b6b452ed8d 100644 (file)
--- a/src/nm.c
+++ b/src/nm.c
@@ -1438,6 +1438,7 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
   free (demangle_buffer);
 #endif
   /* Now we know the exact number.  */
+  size_t nentries_orig = nentries;
   nentries = nentries_used;
 
   /* Sort the entries according to the users wishes.  */
@@ -1472,7 +1473,7 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
     }
 
   /* Free all memory.  */
-  if (nentries * sizeof (sym_mem[0]) >= MAX_STACK_ALLOC)
+  if (nentries_orig * sizeof (sym_mem[0]) >= MAX_STACK_ALLOC)
     free (sym_mem);
 
   obstack_free (&whereob, NULL);