public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] libdw: Fix dwarf_getscopes memory leak on error
Date: Wed, 22 Feb 2023 23:39:01 +0100	[thread overview]
Message-ID: <20230222223901.1089881-1-mark@klomp.org> (raw)

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         | 5 +++++
 libdw/dwarf_getscopes.c | 4 +++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index e0cd8f21..fefc53af 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -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-14  Mark Wielaard  <mark@klomp.org>
 
 	* dwarf_getlocation.c (__libdw_intern_expression): Correct check
diff --git a/libdw/dwarf_getscopes.c b/libdw/dwarf_getscopes.c
index 833f1ac5..ce073b13 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;
 }
-- 
2.31.1


             reply	other threads:[~2023-02-22 22:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-22 22:39 Mark Wielaard [this message]
2023-02-28 11:36 ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230222223901.1089881-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).