]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdw: Fix dwarf_getscopes memory leak on error
authorMark Wielaard <mark@klomp.org>
Wed, 22 Feb 2023 22:34:00 +0000 (23:34 +0100)
committerMark Wielaard <mark@klomp.org>
Tue, 28 Feb 2023 11:35:15 +0000 (12:35 +0100)
When there is an error in dwarf_getscopes after the initial scopes
have been allocated, e.g. when looking for the inlined scopes, then
the scopes would leak. Fix this by explicitly free the scopes on error.

https://sourceware.org/bugzilla/show_bug.cgi?id=29434

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

index 5e60f786d625acb3013bd0935996c504e5df47d1..406310ef3a5f0fd924fcc8b058820bfc62e402fb 100644 (file)
@@ -1,3 +1,8 @@
+2023-02-22  Mark Wielaard  <mark@klomp.org>
+
+       * dwarf_getscopes.c (origin_match): Don't free a->scopes.
+       (dwarf_getscopes): Free a->scopes on error.
+
 2023-02-20  Mark Wielaard  <mark@klomp.org>
 
        * dwarf_begin_elf.c (check_section): Use elf_rawdata.
index 833f1ac504d2608fa4ad4a3dc3566c5ed89178b8..ce073b135d544f80e580ddf17287b481455a40cb 100644 (file)
@@ -101,7 +101,7 @@ origin_match (unsigned int depth, struct Dwarf_Die_Chain *die, void *arg)
   Dwarf_Die *scopes = realloc (a->scopes, nscopes * sizeof scopes[0]);
   if (scopes == NULL)
     {
-      free (a->scopes);
+      /* a->scopes will be freed by dwarf_getscopes on error.  */
       __libdw_seterrno (DWARF_E_NOMEM);
       return -1;
     }
@@ -200,6 +200,8 @@ dwarf_getscopes (Dwarf_Die *cudie, Dwarf_Addr pc, Dwarf_Die **scopes)
 
   if (result > 0)
     *scopes = a.scopes;
+  else if (result < 0)
+    free (a.scopes);
 
   return result;
 }