From patchwork Fri Jul 14 03:35:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 120206 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp2247177vqm; Thu, 13 Jul 2023 20:35:55 -0700 (PDT) X-Google-Smtp-Source: APBJJlGM7IEz8K8eHQaRCEp0d8SNWHbq2ORIwdLU2oKRiDMsNSXQHNUHdRyCc7fdvL5yJLeqhfaQ X-Received: by 2002:a05:6402:164d:b0:51d:e2c4:f94a with SMTP id s13-20020a056402164d00b0051de2c4f94amr3124376edx.20.1689305755773; Thu, 13 Jul 2023 20:35:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689305755; cv=none; d=google.com; s=arc-20160816; b=Iifbrarh5AWn82kjrv5Gk6+aJTXjEKK32lUJXxAUJo+SykqgWUxaduyoI+pv36ce+n xkRnUxMT63+8k+4GPxDXFpVQkXaQoU0c8iEgnIy0/3T2W/2VO1V31r6V7+W5VY6uDPRT 1/PbxBjGQ/1mOqBUTvD4HcshxKqe1YfZpnp7RURFGcI7S8u5VziOzZ/To6G86ZjMTy11 0ez8KmOybNhbwYCI+o2XGyPDm6ocn+nkfcAQKggIEhbInNFmebupnBWcpGlX6hZa8rWe lZiT5eSSX9UcLiaFe0rGM6V+cf9sJxguiCPt5kcWD50LpKJvApoug7YAoqiZbXuaw/K7 kCDw== 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=Dspji7+bRL2xfbCqPNJXvGayqSwefyH3UQhh7zS8Plw=; fh=HNIqzTA9Dp00ovCAyCdXfuEvWDT00OlFxFRPLybXRis=; b=PqEqlIl9HKu8cdJUVrOUXUu9qPCLwpH2sJJ5iPISI1t7UGSw9bwZuvLsdoNymDE/YK r7sgmW9T0Aa5K0/Mzfm3hVBqq+NoUFFgmBRLt5uiiRMv6uUVrF0/+9XKowLvmW3g+3Hh D7H+omobvV3dol/5j9kv0cqWI8cBDqT/+afaMTMf7Wa8cbh//I9PTySkCukZqFYov6P5 +jh2cdD24QbqGsudA/IbxI35uGOcF5HcucujQtUQX4BG62g7c+jxIQMu+afP7Gs6VUXJ YSWt8kvFbIPAa8frY9hbPgxLMkEgTDhTauET3YRIrSntl4JI0vifnC0n5gslP8guospu lMfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=RfbDN9du; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b3-20020aa7cd03000000b0051be8be77afsi8129012edw.145.2023.07.13.20.35.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 20:35:55 -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=RfbDN9du; 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 9F061385828E for ; Fri, 14 Jul 2023 03:35:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9F061385828E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689305754; bh=Dspji7+bRL2xfbCqPNJXvGayqSwefyH3UQhh7zS8Plw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=RfbDN9duZ/FylbtVpWg+CTievsvVlOVK7Url1m0JR10WZ09s8RPZd0an5p6Gdy0EA /ll1oyA5VOeJ31elAQJoWip99dhC9SZA4u75BDTV7wjbkv/Q1RZLBdbraK+/8jS5lF Drmk74rVjEYZGM2kplDQsBb/Wj99p2rm1sz64w1I= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 7BE293858C66 for ; Fri, 14 Jul 2023 03:35:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BE293858C66 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1b8b318c5cfso10944505ad.1 for ; Thu, 13 Jul 2023 20:35:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689305723; x=1691897723; 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=Dspji7+bRL2xfbCqPNJXvGayqSwefyH3UQhh7zS8Plw=; b=Uz06RJsFjHJfD22lkMaJiAWpm8I+GRvmAIktCzNOZr7X+uBpYqcPg7zy/l5Kr919uJ ECkUue6BYO2L1aj69sJtBLiqHJki/YJ5Bmi4Wp1lE74MHzYF7px+NLQ86rrz4cvQ2kY1 Qaec6ZeYAIlzu0sguNhjGWY13E4YjeeF0yZ7di/yikZaroytQYSrLULwf/zRXkyXdHv0 0o9ajhS8jN7SnWJImFuYTSEbv5FYzgeXZD+viA1boZX3HxrcHozuP+Rn+jUA0qQLrdKY q6rtg9INEQB6x+zkNuBlPOIu8o08jwOBTbBm9STBSIlSAt+hL9U7PVx4bbNvGSMFeEX1 /YYA== X-Gm-Message-State: ABy/qLYOMXndi0XDZRlQiNYrhr89i4UMnv+Nrf8Qi47yR/jl9dyaxz33 fweYyMCpoWsm8mWzzApHf+K98jVWe3g= X-Received: by 2002:a17:902:f68c:b0:1b5:91:4693 with SMTP id l12-20020a170902f68c00b001b500914693mr3820551plg.1.1689305723029; Thu, 13 Jul 2023 20:35:23 -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 13-20020a170902c24d00b001b8896cd57bsm6577341plg.269.2023.07.13.20.35.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 20:35:22 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 3EBCC1142A51; Fri, 14 Jul 2023 13:05:20 +0930 (ACST) Date: Fri, 14 Jul 2023 13:05:20 +0930 To: binutils@sourceware.org Subject: More tidies to objcopy archive handling Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.5 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1771365472454905030 X-GMAIL-MSGID: 1771365472454905030 This makes sure copy_archive exits with ibfd and obfd closed. Error paths didn't do that, leading to memory leaks. None of this matters very much. * objcopy.c (copy_archive): bfd_close ibfd and obfd on error return paths. Remove braces around "list" free. (copy_file): Don't close invalid file descriptor. diff --git a/binutils/objcopy.c b/binutils/objcopy.c index c931f67a224..beb655cafc2 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -3615,9 +3615,11 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, } *list, *l; bfd **ptr = &obfd->archive_head; bfd *this_element; - char *dir; + char *dir = NULL; char *filename; + list = NULL; + /* PR 24281: It is not clear what should happen when copying a thin archive. One part is straight forward - if the output archive is in a different directory from the input archive then any relative paths in the library @@ -3636,7 +3638,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, bfd_set_error (bfd_error_invalid_operation); bfd_nonfatal_message (NULL, ibfd, NULL, _("sorry: copying thin archives is not currently supported")); - return; + goto cleanup_and_exit; } /* Make a temp directory to hold the contents. */ @@ -3654,8 +3656,6 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, if (deterministic) obfd->flags |= BFD_DETERMINISTIC_OUTPUT; - list = NULL; - this_element = bfd_openr_next_archived_file (ibfd, NULL); if (!bfd_set_format (obfd, bfd_get_format (ibfd))) @@ -3797,44 +3797,46 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, } *ptr = NULL; + cleanup_and_exit: filename = xstrdup (bfd_get_filename (obfd)); if (!(status == 0 ? bfd_close : bfd_close_all_done) (obfd)) { + if (!status) + bfd_nonfatal_message (filename, NULL, NULL, NULL); status = 1; - bfd_nonfatal_message (filename, NULL, NULL, NULL); } free (filename); filename = xstrdup (bfd_get_filename (ibfd)); if (!bfd_close (ibfd)) { + if (!status) + bfd_nonfatal_message (filename, NULL, NULL, NULL); status = 1; - bfd_nonfatal_message (filename, NULL, NULL, NULL); } free (filename); - cleanup_and_exit: /* Delete all the files that we opened. */ - { - struct name_list * next; - - for (l = list; l != NULL; l = next) - { - if (l->obfd == NULL) - rmdir (l->name); - else - { - bfd_close (l->obfd); - unlink (l->name); - } - free ((char *) l->name); - next = l->next; - free (l); - } - } + struct name_list *next; + for (l = list; l != NULL; l = next) + { + if (l->obfd == NULL) + rmdir (l->name); + else + { + bfd_close (l->obfd); + unlink (l->name); + } + free ((char *) l->name); + next = l->next; + free (l); + } - rmdir (dir); - free (dir); + if (dir) + { + rmdir (dir); + free (dir); + } } /* The top-level control. */ @@ -3933,7 +3935,8 @@ copy_file (const char *input_filename, const char *output_filename, int ofd, if (obfd == NULL) { - close (ofd); + if (ofd >= 0) + close (ofd); bfd_nonfatal_message (output_filename, NULL, NULL, NULL); bfd_close (ibfd); status = 1; @@ -3966,7 +3969,8 @@ copy_file (const char *input_filename, const char *output_filename, int ofd, if (obfd == NULL) { - close (ofd); + if (ofd >= 0) + close (ofd); bfd_nonfatal_message (output_filename, NULL, NULL, NULL); bfd_close (ibfd); status = 1;