From patchwork Mon Apr 24 11:31:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 86999 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2672669vqo; Mon, 24 Apr 2023 04:32:08 -0700 (PDT) X-Google-Smtp-Source: AKy350aM5zzS24KnwbYSpafT3aTOhYXl1CW6DHi3lMjCVphTV9Wx9Xn3t+6/WbYHfAcW33aYuc8F X-Received: by 2002:a17:907:d86:b0:955:34a5:ae49 with SMTP id go6-20020a1709070d8600b0095534a5ae49mr11116325ejc.54.1682335928627; Mon, 24 Apr 2023 04:32:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682335928; cv=none; d=google.com; s=arc-20160816; b=sYuCU4xoG7ZjVTwkJaXfiJ8wqG7tPGd8F1IZkEC6ZPxp53y4NjhJCyj3+Zo4QFGtpQ afO54ET/VG8IjwDz3b/nxLECF19ZI7D1i5xPNcNJxiwVaGICz19nToEkLZ3Wr5JrcIG2 2sS3pBghj7SrsVMKf+B7+MBUxfz7lUDz87zKD9O/uZgqq2mTZOWVafdNgf+Y9Drvt/dX /ZcMST/p913Kw6HBnuwIvaSesP/yeHPM87v1J/4FXD9BXV3ZIo0gg99iQ1iIYFlxA54k iW7rcOaAxY0qH6ONV5MT6jZwr03hDdbgSoo93pDHnI8hYJRK75aYkm4hsCQNDhk0J8R0 XJRQ== 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=yWJxxpT7KP5VKeAtnsv7sMsNH6K3eDWCmbhgLYnM/6U=; b=st++WNtYkcrRikLXMgq50R3jkCnrPBbWWyIJ72pqtv6/cdILXT0plC8ETGqAeoRu13 szvfcIJ8JMcpY+FtVDLWa1JgV82eqFanplj/pJP4vM1swDtb5FHVtxayOJCR8f16XmqK oBtmvXgNjDGaa/6SFiIIamCRlVzZSBX0zCyXUUSG6NdUtQQ1740qRKMX6OZHc4LYnsFN KZR5J17WADwP5l/rXrBV9zRTPPp+ZlY3DRObKS9D/jyiwWHyLXf6+w/dLY7D0b2UXT1m yH0tNlf21onw/+3+moED4mDhnuKdQJjM+pMA/yBI/HBB/4zQ6F0i5s5V+cJC6Jxk3GMh 6Axg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=fH1AS1DT; 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 n10-20020a17090695ca00b0094efa67a2efsi4753113ejy.820.2023.04.24.04.32.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Apr 2023 04:32:08 -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=fH1AS1DT; 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 40410385840D for ; Mon, 24 Apr 2023 11:32:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 40410385840D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1682335927; bh=yWJxxpT7KP5VKeAtnsv7sMsNH6K3eDWCmbhgLYnM/6U=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fH1AS1DT47+SNCTEfQcIPqRMrLiBf7isl0Qk1ZI8fX3m3HjHnm4QeR//f+PKat/B9 j1x8mXaYU0VtoTtU6LOzZoWsvNfnd31kkhbgG2lGltAc4b3LrEi3iQhRAU1tMq0Dc4 XuowrixgSTKqxJtBZdBW78B0C++vc0wH8zROQKJk= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by sourceware.org (Postfix) with ESMTPS id D2E8A3858D28 for ; Mon, 24 Apr 2023 11:31:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D2E8A3858D28 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1a52667955dso48486845ad.1 for ; Mon, 24 Apr 2023 04:31:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682335915; x=1684927915; 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=yWJxxpT7KP5VKeAtnsv7sMsNH6K3eDWCmbhgLYnM/6U=; b=U/XUMB+VQmZpHsamIxc3lDSsI/C8er629mtQCecs4Gd6h9D1DgYCPIrcJpH6N1fnD5 PP4JZ171bz7YvnLBikMQdbOwa8WTVjOhaulv0pRLwApx3vIDQz3W5AqFKiRqI0oMrRK1 TrVK2e+tGH3/jNxHQHSmzUGcX/esRIIFUg0hoZJyVi2pd32dO2AhqDYtdHhKFnLeKjq8 WN65OZJf1ykbfut10Ymoaa7wxRPq4znY3vFwtvtmm6AfKk41SaS8OMbRjGdB4lqMOTqA +NIp2ilUrLhLL4Uu9sBwvfmecJ5iE8gvB7tAYo3W4iPKn1XiqAtcKkdA6BxDEn53lsse 2+mw== X-Gm-Message-State: AAQBX9eJh7J69xA/i02kRD/ietakgg74ofE9IoIuMwoO4N6FYOtCebAi 22Ko/s64WnK1xEYVQEF+JMEqPk+xoxU= X-Received: by 2002:a17:903:22ce:b0:1a9:6397:9919 with SMTP id y14-20020a17090322ce00b001a963979919mr7852764plg.48.1682335915131; Mon, 24 Apr 2023 04:31:55 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:2b3:c4ca:1712:a671]) by smtp.gmail.com with ESMTPSA id x15-20020a17090a46cf00b00247164c1947sm10025544pjg.0.2023.04.24.04.31.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Apr 2023 04:31:54 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 0C7ED1142C24; Mon, 24 Apr 2023 21:01:52 +0930 (ACST) Date: Mon, 24 Apr 2023 21:01:52 +0930 To: binutils@sourceware.org Subject: objcopy of archives tidy Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.4 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764057078558649584?= X-GMAIL-MSGID: =?utf-8?q?1764057078558649584?= This makes sure the input element bfd is closed before exiting the loop copying elements. * objcopy.c (copy_archive): Rename output_bfd to output_element. Localise last_element. Close this_element in more error cases. diff --git a/binutils/objcopy.c b/binutils/objcopy.c index d4fc644640c..49d54bff9b2 100644 --- a/binutils/objcopy.c +++ b/binutils/objcopy.c @@ -3625,8 +3625,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, while (!status && this_element != NULL) { char *output_name; - bfd *output_bfd; - bfd *last_element; + bfd *output_element; struct stat buf; int stat_status = 0; bool del = true; @@ -3638,6 +3637,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, { non_fatal (_("illegal pathname found in archive member: %s"), bfd_get_filename (this_element)); + bfd_close (this_element); status = 1; goto cleanup_and_exit; } @@ -3656,6 +3656,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, { non_fatal (_("cannot create tempdir for archive copying (error: %s)"), strerror (errno)); + bfd_close (this_element); status = 1; goto cleanup_and_exit; } @@ -3693,20 +3694,21 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, /* PR binutils/3110: Cope with archives containing multiple target types. */ if (force_output_target || !ok_object) - output_bfd = bfd_openw (output_name, output_target); + output_element = bfd_openw (output_name, output_target); else - output_bfd = bfd_openw (output_name, bfd_get_target (this_element)); + output_element = bfd_openw (output_name, bfd_get_target (this_element)); - if (output_bfd == NULL) + if (output_element == NULL) { bfd_nonfatal_message (output_name, NULL, NULL, NULL); + bfd_close (this_element); status = 1; goto cleanup_and_exit; } if (ok_object) { - del = !copy_object (this_element, output_bfd, input_arch); + del = !copy_object (this_element, output_element, input_arch); if (del && bfd_get_arch (this_element) == bfd_arch_unknown) /* Try again as an unknown object file. */ @@ -3714,10 +3716,10 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, } if (!ok_object) - del = !copy_unknown_object (this_element, output_bfd); + del = !copy_unknown_object (this_element, output_element); if (!(ok_object && !del && !status - ? bfd_close : bfd_close_all_done) (output_bfd)) + ? bfd_close : bfd_close_all_done) (output_element)) { bfd_nonfatal_message (output_name, NULL, NULL, NULL); /* Error in new object file. Don't change archive. */ @@ -3729,23 +3731,24 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target, unlink (output_name); status = 1; } + + if (status) + bfd_close (this_element); else { if (preserve_dates && stat_status == 0) set_times (output_name, &buf); - /* Open the newly output file and attach to our list. */ - output_bfd = bfd_openr (output_name, output_target); - - l->obfd = output_bfd; + /* Open the newly created output file and attach to our list. */ + output_element = bfd_openr (output_name, output_target); - *ptr = output_bfd; - ptr = &output_bfd->archive_next; + l->obfd = output_element; - last_element = this_element; + *ptr = output_element; + ptr = &output_element->archive_next; + bfd *last_element = this_element; this_element = bfd_openr_next_archived_file (ibfd, last_element); - bfd_close (last_element); } }