From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-x42f.google.com with SMTP id 65so11567618pfx.9 for ; Wed, 21 Dec 2022 13:28:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=ZD/ZkgFxpiiOXXijWXQFUwQ4JxIvaystNouV+Vg/Ezw=; b=VEKEWichlWW8C+ZamIE5pqDnDKp8+ciXJJuuHBenDtl04DrXpgK2RXR8bKDxPP8FZR 0SUPCX9gwByxpFVfQXtqml/Z8xyMHPIjqLVXktTMJxz2zQhypnVrMJfwuue558LyYq2a b/NJR/KoqEFjeZ9Y5UK50+0kUF+FTotdD6DJaGnG2HVWTRvbnYNBY7L3mNxRJZFRWaaH klLVuw2r+RAv59R4P2jFjnAMehGLFUqbpwihvQ7bnD+yCYcumsQYIKOhSknx/myz4XO8 le6mQZXjEvlLtuzcS8nBipuJx61GcBZuCv6qroVvh9xHgOnxjAJSaRKNKQ6NPBc6r8sj zA7g== 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-Google-Smtp-Source: AMrXdXuWVi0Pzw/PEobiUS3BzmMTm4D+B4FPWDFtWx58wN7FzgZ44ml2rhHCPgjWPsAdkTymKByR7w== 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 From: Alan Modra To: binutils@sourceware.org Subject: PR29925, Memory leak in find_abstract_instance Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 List-Id: 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: -- Alan Modra Australia Development Lab, IBM