From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id C9C3E385842D; Mon, 24 Apr 2023 11:33:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C9C3E385842D Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] objcopy of archives tidy X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 596a7c72b2eb3da8247651805f4afc2c0da4c275 X-Git-Newrev: c1eb3cd205921fb7af668f2032ab2c7142c4f24c Message-Id: <20230424113304.C9C3E385842D@sourceware.org> Date: Mon, 24 Apr 2023 11:33:04 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Apr 2023 11:33:04 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc1eb3cd20592= 1fb7af668f2032ab2c7142c4f24c commit c1eb3cd205921fb7af668f2032ab2c7142c4f24c Author: Alan Modra Date: Sat Apr 22 16:01:02 2023 +0930 objcopy of archives tidy =20 This makes sure the input element bfd is closed before exiting the loop copying elements. =20 * objcopy.c (copy_archive): Rename output_bfd to output_element. Localise last_element. Close this_element in more error cases. Diff: --- binutils/objcopy.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) 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 *outpu= t_target, while (!status && this_element !=3D NULL) { char *output_name; - bfd *output_bfd; - bfd *last_element; + bfd *output_element; struct stat buf; int stat_status =3D 0; bool del =3D true; @@ -3638,6 +3637,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *outpu= t_target, { non_fatal (_("illegal pathname found in archive member: %s"), bfd_get_filename (this_element)); + bfd_close (this_element); status =3D 1; goto cleanup_and_exit; } @@ -3656,6 +3656,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *outpu= t_target, { non_fatal (_("cannot create tempdir for archive copying (error: %s)= "), strerror (errno)); + bfd_close (this_element); status =3D 1; goto cleanup_and_exit; } @@ -3693,20 +3694,21 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *out= put_target, /* PR binutils/3110: Cope with archives containing multiple target types. */ if (force_output_target || !ok_object) - output_bfd =3D bfd_openw (output_name, output_target); + output_element =3D bfd_openw (output_name, output_target); else - output_bfd =3D bfd_openw (output_name, bfd_get_target (this_element)); + output_element =3D bfd_openw (output_name, bfd_get_target (this_element)); =20 - if (output_bfd =3D=3D NULL) + if (output_element =3D=3D NULL) { bfd_nonfatal_message (output_name, NULL, NULL, NULL); + bfd_close (this_element); status =3D 1; goto cleanup_and_exit; } =20 if (ok_object) { - del =3D !copy_object (this_element, output_bfd, input_arch); + del =3D !copy_object (this_element, output_element, input_arch); =20 if (del && bfd_get_arch (this_element) =3D=3D bfd_arch_unknown) /* Try again as an unknown object file. */ @@ -3714,10 +3716,10 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *out= put_target, } =20 if (!ok_object) - del =3D !copy_unknown_object (this_element, output_bfd); + del =3D !copy_unknown_object (this_element, output_element); =20 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 *out= put_target, unlink (output_name); status =3D 1; } + + if (status) + bfd_close (this_element); else { if (preserve_dates && stat_status =3D=3D 0) set_times (output_name, &buf); =20 - /* Open the newly output file and attach to our list. */ - output_bfd =3D bfd_openr (output_name, output_target); - - l->obfd =3D output_bfd; + /* Open the newly created output file and attach to our list. */ + output_element =3D bfd_openr (output_name, output_target); =20 - *ptr =3D output_bfd; - ptr =3D &output_bfd->archive_next; + l->obfd =3D output_element; =20 - last_element =3D this_element; + *ptr =3D output_element; + ptr =3D &output_element->archive_next; =20 + bfd *last_element =3D this_element; this_element =3D bfd_openr_next_archived_file (ibfd, last_element); - bfd_close (last_element); } }