From: Mark Wielaard Date: Wed, 22 Feb 2023 22:34:00 +0000 (+0100) Subject: libdw: Fix dwarf_getscopes memory leak on error X-Git-Tag: elfutils-0.189~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e24d8a4a3ea106608bb3e8d33c4639cf71d0f08d;p=thirdparty%2Felfutils.git libdw: Fix dwarf_getscopes memory leak on error 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 --- diff --git a/libdw/ChangeLog b/libdw/ChangeLog index 5e60f786d..406310ef3 100644 --- a/libdw/ChangeLog +++ b/libdw/ChangeLog @@ -1,3 +1,8 @@ +2023-02-22 Mark Wielaard + + * dwarf_getscopes.c (origin_match): Don't free a->scopes. + (dwarf_getscopes): Free a->scopes on error. + 2023-02-20 Mark Wielaard * dwarf_begin_elf.c (check_section): Use elf_rawdata. diff --git a/libdw/dwarf_getscopes.c b/libdw/dwarf_getscopes.c index 833f1ac50..ce073b135 100644 --- a/libdw/dwarf_getscopes.c +++ b/libdw/dwarf_getscopes.c @@ -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; }