From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id B9D863858D3C for ; Wed, 22 Feb 2023 22:39:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B9D863858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from csb.redhat.com (deer0x03.wildebeest.org [172.31.17.133]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 269303039EBD; Wed, 22 Feb 2023 23:39:08 +0100 (CET) Received: by csb.redhat.com (Postfix, from userid 10916) id 03B63CB993; Wed, 22 Feb 2023 23:39:08 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: Mark Wielaard Subject: [PATCH] libdw: Fix dwarf_getscopes memory leak on error Date: Wed, 22 Feb 2023 23:39:01 +0100 Message-Id: <20230222223901.1089881-1-mark@klomp.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3037.3 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 --- 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 + + * dwarf_getscopes.c (origin_match): Don't free a->scopes. + (dwarf_getscopes): Free a->scopes on error. + 2023-02-14 Mark Wielaard * 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