From patchwork Wed Dec 21 21:28:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 35528 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp3750230wrn; Wed, 21 Dec 2022 13:28:28 -0800 (PST) X-Google-Smtp-Source: AMrXdXvmJc4h0c6sLhbzsQaFkSlmCwHYYchKGn7QFmcZtyQPMTf0eATXHrivPJS/TTzHGfL0kq6s X-Received: by 2002:a17:906:819:b0:7ad:e67d:f15c with SMTP id e25-20020a170906081900b007ade67df15cmr6306024ejd.48.1671658108808; Wed, 21 Dec 2022 13:28:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671658108; cv=none; d=google.com; s=arc-20160816; b=yS/Oz6snUguMkEL+wLGerNXvhQAYzK1TlBY4zr5XIzNleYTYDqsPIBAvt7qrIOkNE3 //ClGfeHb/kLvqrbJigVwtd/H62b+2nNBm+0xOgpU7Podxhli1kksx/imFeGDIvyqoLL pIWLXTE4XxUqSNvdLnCEmCSZy4bDbJuRhlT1ALod/jFgs+SDQ0Vcq2naUH43IQT3zsvT qWDACET7tw6AO2vcz1CLjgttEXTo1f9zFmENDWyrcDse5rHdIZYKPANIkP2uIbvNvp2K 4zWKDsyxTp3yY6T4rFSAWx1/AUoEZMW4PCgeac0meJTfPgkc4JxNY35WZEVCzuthcXzA mjrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ZD/ZkgFxpiiOXXijWXQFUwQ4JxIvaystNouV+Vg/Ezw=; b=N8/DnIQWMupIPyYSRWkMTN2y+f/8DJPS3i4CvE9upNtSxi2ZQHTzVmqqAtcqutYte5 vxL9aw41zEI8tSrr8mh6PlzZMYckBU6C2/2Os+AtAj+difYH2NN5/wShQIZsMhBbYiLS Z4tpGKLAFHzp+WMcqMMRmg6VCmhnpRIduobV+rr7Q+5J4qpPg2pjjgXX1/8X9DErdKY+ Pv9VXbviqOMji8vw0WQBvf8yzA6EuvApLyoqzoylLZ4xr3LRsf2ITBJkiDasp5bhxW/R ZFx2RDqPc1ocyTJcJQTvQ+XHnXgoKBsJCIYKBkzKkAhZU/wfWOyKH/HbxltfOYPfKs7i 3p1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=cB0Pa0AG; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id l16-20020a056402255000b004614acc0706si16464876edb.250.2022.12.21.13.28.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Dec 2022 13:28:28 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=cB0Pa0AG; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 98B803857BB3 for ; Wed, 21 Dec 2022 21:28:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 98B803857BB3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671658107; bh=ZD/ZkgFxpiiOXXijWXQFUwQ4JxIvaystNouV+Vg/Ezw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=cB0Pa0AGwLqYXq2j06fT5RRD5UtJcKhDOT1IdLCfW8YhFfX1H+VLu9TFDRBI0R1Vb AZDVSi4a8ujHxuohy+dn/QTdv+2cM9+sdEPNvBtJ7A4xa9btjyd1IuO5mY3dIyzBs5 0ZlQMhgVsdAVhFYiCbCb4koDEJBwtWOBXOCVCm+s= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 452F53858D1E for ; Wed, 21 Dec 2022 21:28:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 452F53858D1E Received: by mail-pf1-x42f.google.com with SMTP id 65so11567618pfx.9 for ; Wed, 21 Dec 2022 13:28:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZD/ZkgFxpiiOXXijWXQFUwQ4JxIvaystNouV+Vg/Ezw=; b=aZA+NoHIZWIxHZ/aMd3dFy66RWnX7k+tk22RFCw4X2xmu58JsAlD+Y7+mIQawJDQ/w f4u9qs8tRCo6r3nHJNqHzoMre23HOA2q2MS+0Zo7rZXFLSy3hJQldZ1/AXqurLOO2k+D V002GhOzpN7n2gKD3hoGXEUyBkGpiQ8HV6LZ8cBcQsVP4WF8avqSCtqijTWe8caMObYt AOVH0ZCGgsxZ9rqRTBjo2dty4hCYWwiLZAycn6x4ZYuNqpg+LTlY6gzW9D5nzcrPAr9k CUxsn0wF0ucfHfcnj/OXwmSi1Zg4ncJ/aEF7Xdw23wEF4EiuEsdsWA5CT53upCkvUMO2 R9zQ== X-Gm-Message-State: AFqh2kp7SxfeBm2MFeHbyOhXkO68MXRqGv4ZQVYHNF8l0xSwPPgzgug0 SPTNLZ/HaDICDAi1RxHHtBUjjaXkSWE= X-Received: by 2002:aa7:93ae:0:b0:574:f1bc:bbee with SMTP id x14-20020aa793ae000000b00574f1bcbbeemr4002538pff.31.1671658099076; Wed, 21 Dec 2022 13:28:19 -0800 (PST) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id u66-20020a626045000000b005771f5ea2ebsm11011496pfb.135.2022.12.21.13.28.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Dec 2022 13:28:18 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 3C9521141C5C; Thu, 22 Dec 2022 07:58:16 +1030 (ACDT) Date: Thu, 22 Dec 2022 07:58:16 +1030 To: binutils@sourceware.org Subject: PR29925, Memory leak in find_abstract_instance Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752860572798218770?= X-GMAIL-MSGID: =?utf-8?q?1752860572798218770?= The testcase in the PR had a variable with both DW_AT_decl_file and DW_AT_specification, where the DW_AT_specification also specified DW_AT_decl_file. This leads to a memory leak as the file name is malloced and duplicates are not expected. I've also changed find_abstract_instance to not use a temp for "name", because that can result in a change in behaviour from the usual last of duplicate attributes wins. PR 29925 * dwarf2.c (find_abstract_instance): Delete "name" variable. Free *filename_ptr before assigning new file name. (scan_unit_for_symbols): Similarly free func->file and var->file before assigning. diff --git a/bfd/dwarf2.c b/bfd/dwarf2.c index 0cd8152ee6e..b608afbc0cf 100644 --- a/bfd/dwarf2.c +++ b/bfd/dwarf2.c @@ -3441,7 +3441,6 @@ find_abstract_instance (struct comp_unit *unit, struct abbrev_info *abbrev; uint64_t die_ref = attr_ptr->u.val; struct attribute attr; - const char *name = NULL; if (recur_count == 100) { @@ -3602,9 +3601,9 @@ find_abstract_instance (struct comp_unit *unit, case DW_AT_name: /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name over DW_AT_name. */ - if (name == NULL && is_str_form (&attr)) + if (*pname == NULL && is_str_form (&attr)) { - name = attr.u.str; + *pname = attr.u.str; if (mangle_style (unit->lang) == 0) *is_linkage = true; } @@ -3612,7 +3611,7 @@ find_abstract_instance (struct comp_unit *unit, case DW_AT_specification: if (is_int_form (&attr) && !find_abstract_instance (unit, &attr, recur_count + 1, - &name, is_linkage, + pname, is_linkage, filename_ptr, linenumber_ptr)) return false; break; @@ -3622,7 +3621,7 @@ find_abstract_instance (struct comp_unit *unit, non-string forms into these attributes. */ if (is_str_form (&attr)) { - name = attr.u.str; + *pname = attr.u.str; *is_linkage = true; } break; @@ -3630,8 +3629,11 @@ find_abstract_instance (struct comp_unit *unit, if (!comp_unit_maybe_decode_line_info (unit)) return false; if (is_int_form (&attr)) - *filename_ptr = concat_filename (unit->line_table, - attr.u.val); + { + free (*filename_ptr); + *filename_ptr = concat_filename (unit->line_table, + attr.u.val); + } break; case DW_AT_decl_line: if (is_int_form (&attr)) @@ -3643,7 +3645,6 @@ find_abstract_instance (struct comp_unit *unit, } } } - *pname = name; return true; } @@ -4139,8 +4140,11 @@ scan_unit_for_symbols (struct comp_unit *unit) case DW_AT_decl_file: if (is_int_form (&attr)) - func->file = concat_filename (unit->line_table, - attr.u.val); + { + free (func->file); + func->file = concat_filename (unit->line_table, + attr.u.val); + } break; case DW_AT_decl_line: @@ -4182,8 +4186,11 @@ scan_unit_for_symbols (struct comp_unit *unit) case DW_AT_decl_file: if (is_int_form (&attr)) - var->file = concat_filename (unit->line_table, - attr.u.val); + { + free (var->file); + var->file = concat_filename (unit->line_table, + attr.u.val); + } break; case DW_AT_decl_line: