From patchwork Thu Mar 30 04:49:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 76882 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp873046vqo; Wed, 29 Mar 2023 21:50:10 -0700 (PDT) X-Google-Smtp-Source: AKy350Zq87BP3fHkJjckcUOZ60aDPYrdkD+cbhvMj3Mp5MsmTBqPNheoMln0Nsgt0PxVs9OUEwf2 X-Received: by 2002:a17:907:2e19:b0:930:9f89:65ef with SMTP id ig25-20020a1709072e1900b009309f8965efmr20629645ejc.11.1680151810393; Wed, 29 Mar 2023 21:50:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680151810; cv=none; d=google.com; s=arc-20160816; b=0HYddaIGzrBw7D2zaLhAugDc8qK5hCffr/Zdx74R12/i7TvZg/rw2EiQPMHkLV/Umx o29u56X5ozJEtdnhkG22SoP316NJM0dRccR+1sjnBYdO8moK8SW7D9tKpBl1ghoP00Jw F1kvJSR5Evwl8/oswZdh1nuiO9TLCac3mYnQRtFE4io1clmxxCe+gBb2GRi/G8uosu1a Kbd9H3wYNmIFSaoT1AaQkdq9bT9/YDGGTjxpSzhN7I4ih9kcn70Sny5LL3qHyH9L5Jlx hLsIjuGH/fFDiOFe4etGjfItl6vxJSN/tX4AlCKHt5BEPjRS1kW9U5uyJjvyR0NpQ4Fs 1Kfw== 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=HFKRzRbgrNVUr3llTOEec4OOq8vK+2cZ85krtz6d/ic=; b=CQ0T5jNA5gB2QcMP/PzMgbAOa24t4M4YOzooxpuxYvdZ8llqNVIJD+kfTXCPjzfvr0 xWrEGxLojBx3UoWchZapw2DNyZd8ShDfr7d807L+fX8sMzWlvPhvrJR80H00mguQ7vMh 7I7RquaIBrJc8wnpJVVBgfarvpgUmFDzaGT+xzUIRobgqG4HjC9F0NDEtbyy8Hl8hqNF MW6sGylRwyvko3TdYc2le7aKsysxLJHZkprtWI2FfuH/SPViyQTG145faOVtENJ4XwBC M00eLFPaH5CHuMZAVznsT+xvJF5yHWzFRrOcjqQEdNspprWFcgTonGgMU9azPVadNVpZ Fnfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=f7tx9Ska; 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 z20-20020a170906815400b0093448e806d3si27725384ejw.85.2023.03.29.21.50.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:50:10 -0700 (PDT) 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=f7tx9Ska; 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 1327B3858C53 for ; Thu, 30 Mar 2023 04:50:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1327B3858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680151809; bh=HFKRzRbgrNVUr3llTOEec4OOq8vK+2cZ85krtz6d/ic=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=f7tx9SkaRx9rzYC/BAtY/QoI5tyvXF6yEcegEXikPKsQVESzRE4Nwqt6mUY0X22+v 5WMwIDjxvuXlu+QXLwobIWw9KhOLPfqYsoU4ZExiidrG3l7y0q2kxKKjYgVvVtQpJh rK3krpc+1RDfhGAAssWUoofgj0nEqfPOc/QMxZ2k= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by sourceware.org (Postfix) with ESMTPS id 2D4533858436 for ; Thu, 30 Mar 2023 04:49:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D4533858436 Received: by mail-pj1-x1029.google.com with SMTP id x15so16168545pjk.2 for ; Wed, 29 Mar 2023 21:49:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680151773; 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=HFKRzRbgrNVUr3llTOEec4OOq8vK+2cZ85krtz6d/ic=; b=XNouROuXZg2FomFCjT/mbFrUnAf3WFrvTHetmdm3F8TpQIRdm4zTS+TTWhmdfhnt8P TiiBzP+OdeQVfVUFw2E7PWSW/IVRfiQb3Azw3qdZUaK7LIHb9GQBlREVj23OauSaMn4U l7qFdNIBQb15M+VWQflKiWPunmF0XvYoht20s0lKrUam80iH4zKBqRcleARNTfFq/kbA ORFu/40ufLqf3wJCfHbQMFK56H4FQmv/uPV3hoFZ/dTW4wbPUwryDhkFQ1KjZrHoN4QV IZuNlms16FFeCgzGfvDaS2Qej/mNYkQJl7RF7vbJYIKVL7W4GV6upoY+lLSLJIeUS15X j6jA== X-Gm-Message-State: AAQBX9cNpt7lqJHFUhCpUwUeEnIAnFQSoi0t1jUJ4wgn3nxFMcJfSC8a 5WxD3a9PBW8D0RRPZgAQDG373WsqYn4= X-Received: by 2002:a05:6a20:7883:b0:df:81de:93dd with SMTP id d3-20020a056a20788300b000df81de93ddmr14221106pzg.44.1680151772763; Wed, 29 Mar 2023 21:49:32 -0700 (PDT) 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 u13-20020aa7838d000000b0059085684b54sm24599294pfm.140.2023.03.29.21.49.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 21:49:32 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id F3D631142E2D; Thu, 30 Mar 2023 15:19:29 +1030 (ACDT) Date: Thu, 30 Mar 2023 15:19:29 +1030 To: binutils@sourceware.org Subject: Tidy leaked objcopy memory Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.1 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?1761766864890741689?= X-GMAIL-MSGID: =?utf-8?q?1761766864890741689?= * objcopy.c (delete_symbol_htabs): Also free symbols. (write_debugging_info): Free strings and syms once written. * wrstabs.c (write_stabs_in_sections_debugging_info): memset entire info struct. Free hash tables before returning. Free syms on error return. diff --git a/binutils/objcopy.c b/binutils/objcopy.c index cf830442b3c..72585ea9ed7 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -1065,6 +1065,10 @@ delete_symbol_htabs (void) htab_delete (weaken_specific_htab); htab_delete (redefine_specific_htab); htab_delete (redefine_specific_reverse_htab); + + free (isympp); + if (osympp != isympp) + free (osympp); } /* Add a symbol to strip_specific_list. */ @@ -4656,6 +4660,7 @@ write_debugging_info (bfd *obfd, void *dhandle, bfd_size_type symsize, stringsize; asection *stabsec, *stabstrsec; flagword flags; + bool ret; if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms, &symsize, &strings, @@ -4682,17 +4687,19 @@ write_debugging_info (bfd *obfd, void *dhandle, the next thing the caller is going to do is copy over the real sections. We may someday have to split the contents setting out of this function. */ + ret = true; if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize) || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0, stringsize)) { bfd_nonfatal_message (NULL, obfd, NULL, _("can't set debugging section contents")); - free (strings); - return false; + ret = false; } - return true; + free (strings); + free (syms); + return ret; } bfd_nonfatal_message (NULL, obfd, NULL, diff --git a/binutils/wrstabs.c b/binutils/wrstabs.c index 2df2b041434..53ade2eafbd 100644 --- a/binutils/wrstabs.c +++ b/binutils/wrstabs.c @@ -469,16 +469,18 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, struct string_hash_entry *h; bfd_byte *p; + memset (&info, 0, sizeof info); info.abfd = abfd; - info.symbols_size = 0; info.symbols_alloc = 500; info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc); - info.strings = NULL; - info.last_string = NULL; /* Reserve 1 byte for a null byte. */ info.strings_size = 1; + info.type_index = 1; + info.so_offset = -1; + info.fun_offset = -1; + info.pending_lbrac = (bfd_vma) -1; if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc, sizeof (struct string_hash_entry)) @@ -487,38 +489,28 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, { non_fatal ("bfd_hash_table_init_failed: %s", bfd_errmsg (bfd_get_error ())); - return false; + goto fail; } - info.type_stack = NULL; - info.type_index = 1; - memset (&info.type_cache, 0, sizeof info.type_cache); - info.so_offset = -1; - info.fun_offset = -1; - info.last_text_address = 0; - info.nesting = 0; - info.fnaddr = 0; - info.pending_lbrac = (bfd_vma) -1; - /* The initial symbol holds the string size. */ if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL)) - return false; + goto fail; /* Output an initial N_SO symbol. */ info.so_offset = info.symbols_size; if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd))) - return false; + goto fail; if (! debug_write (dhandle, &stab_fns, (void *) &info)) - return false; + goto fail; if (info.pending_lbrac != (bfd_vma) -1) - return false; + goto fail; /* Output a trailing N_SO. */ if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address, (const char *) NULL)) - return false; + goto fail; /* Put the string size in the initial symbol. */ bfd_put_32 (abfd, info.strings_size, info.symbols + 8); @@ -537,7 +529,17 @@ write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle, p += strlen ((char *) p) + 1; } + bfd_hash_table_free (&info.typedef_hash.table); + bfd_hash_table_free (&info.strhash.table); return true; + + fail: + if (info.typedef_hash.table.memory) + bfd_hash_table_free (&info.typedef_hash.table); + if (info.strhash.table.memory) + bfd_hash_table_free (&info.strhash.table); + free (info.symbols); + return false; } /* Start writing out information for a compilation unit. */