From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 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-pl1-x62a.google.com with SMTP id d9443c01a7336-1a52667955dso48486845ad.1 for ; Mon, 24 Apr 2023 04:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682335915; x=1684927915; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=yWJxxpT7KP5VKeAtnsv7sMsNH6K3eDWCmbhgLYnM/6U=; b=maoX/3avfKTOeZ4N4o5WXl2YNRz7LI/UBmayIlemrSQ9OXzkMds2zbCJSbLxHyfBkn qSh6704nlMojQGp6aZTN0GDxwHUFlDSWWOMloBe9XjkVcRElP77a08ZIhuHBLRNoa1Dc ujr0pVvoK8xGbCQuUtW7VuOix03U1gvZxdPdc8fL7ak0+7IKbeJx6/8IMarokFyFhOBN 9AjnRz71bvCFwEr0AXvCthqh/9GFcDPsvGjANYzpt+fz5nEWF/OtCVj9oIElOLIofvKU cUSLDfzYX2ChAnlfF9f8FLjGsVRMFyFiDq3fNalDn5GodF+UDE61bvKgOQoPWVjCZzBT 5IRA== 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-Google-Smtp-Source: AKy350aZ18tAMpImpmcgUZ90zRY8y1NZOOmt3uWKcfwGYr8KbkEic9ydch2070SnRDqGVJbkFm91XQ== 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 From: Alan Modra To: binutils@sourceware.org Subject: objcopy of archives tidy Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 List-Id: 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); } } -- Alan Modra Australia Development Lab, IBM