From patchwork Thu Aug 4 15:13:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: dodji at seketeli dot org X-Patchwork-Id: 56542 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5F075385702C for ; Thu, 4 Aug 2022 15:13:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5F075385702C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1659626007; bh=5k/Fj1kD7gRZUMaWbNtLFnQPpxzymlRlWP+zya+v5xY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Help:List-Subscribe:From:Reply-To:From; b=VjhhZtYNmBkFB11SHSJobt+yoS0UQDBuws+o440JQu5E+0UbHTAaarVuVoWiG+USt CwUpdX+GeHJVYR99lichJh8dMMj3+AR14/sDPQR4VWwzNkJWmCVsV/NGMXi7YLM+Ve YKBLsToTZtv3PKSediPWQ6eTCBuyXKA6MS0dFadM= X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: by sourceware.org (Postfix, from userid 48) id E2891385802E; Thu, 4 Aug 2022 15:13:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2891385802E To: elfutils-devel@sourceware.org Subject: [Bug libdw/29434] Memory leak in `dwarf_getscopes` Date: Thu, 04 Aug 2022 15:13:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: elfutils X-Bugzilla-Component: libdw X-Bugzilla-Version: unspecified X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: mark at klomp dot org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: mark at klomp dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: mark at klomp dot org via Elfutils-devel From: dodji at seketeli dot org Reply-To: mark at klomp dot org Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" https://sourceware.org/bugzilla/show_bug.cgi?id=29434 --- Comment #3 from Mark Wielaard --- (In reply to Pablo Galindo Salgado from comment #2) > > it could in theory also be zero > > In this case, the result was 0 (I am working with Matt) Interesting, I assumed it was on a failure path. My proposed patch would have been: } @@ -198,6 +198,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; } But if the result is zero I don't believe I fully understand yet how the leak happens. diff --git a/libdw/dwarf_getscopes.c b/libdw/dwarf_getscopes.c index 5662eecf..676d62f3 100644 --- a/libdw/dwarf_getscopes.c +++ b/libdw/dwarf_getscopes.c @@ -100,7 +100,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;