public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: close last arfile before processing current arfile
Date: Thu, 12 Dec 2024 16:02:25 +1030	[thread overview]
Message-ID: <Z1p1abDJU2uf4opF@squeak.grove.modra.org> (raw)

This also reduces peak memory a little.

	* dlltool.c (identify_search_archive): Close last_arfile earlier.
	Report an error if bfd_openr_next_archived_file returns the same
	bfd.  Localise variables.
	* nm.c (display_archive): Likewise.
	* objdump.c (display_any_bfd): Likewise.
	* size.c (display_archive): Likewise.

diff --git a/binutils/dlltool.c b/binutils/dlltool.c
index 17e9416a7f1..990082d3b44 100644
--- a/binutils/dlltool.c
+++ b/binutils/dlltool.c
@@ -3322,21 +3322,25 @@ identify_search_archive (bfd * abfd,
 			 void (* operation) (bfd *, bfd *, void *),
 			 void * user_storage)
 {
-  bfd *   arfile = NULL;
-  bfd *   last_arfile = NULL;
-  char ** matching;
+  bfd *last_arfile = NULL;
 
   while (1)
     {
-      arfile = bfd_openr_next_archived_file (abfd, arfile);
-
-      if (arfile == NULL)
+      bfd *arfile = bfd_openr_next_archived_file (abfd, last_arfile);
+      if (arfile == NULL
+	  || arfile == last_arfile)
 	{
+	  if (arfile != NULL)
+	    bfd_set_error (bfd_error_malformed_archive);
 	  if (bfd_get_error () != bfd_error_no_more_archived_files)
 	    bfd_fatal (bfd_get_filename (abfd));
 	  break;
 	}
 
+      if (last_arfile != NULL)
+	bfd_close (last_arfile);
+
+      char **matching;
       if (bfd_check_format_matches (arfile, bfd_object, &matching))
 	(*operation) (arfile, abfd, user_storage);
       else
@@ -3345,24 +3349,11 @@ identify_search_archive (bfd * abfd,
 	  free (matching);
 	}
 
-      if (last_arfile != NULL)
-	{
-	  bfd_close (last_arfile);
-	  /* PR 17512: file: 8b2168d4.  */
-	  if (last_arfile == arfile)
-	    {
-	      last_arfile = NULL;
-	      break;
-	    }
-	}
-
       last_arfile = arfile;
     }
 
   if (last_arfile != NULL)
-    {
-      bfd_close (last_arfile);
-    }
+    bfd_close (last_arfile);
 }
 
 /* Call the identify_search_section() function for each section of this
diff --git a/binutils/nm.c b/binutils/nm.c
index 87e8305b3f8..aabaea46f2a 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -1566,26 +1566,29 @@ set_print_format (bfd *file)
 static void
 display_archive (bfd *file)
 {
-  bfd *arfile = NULL;
-  bfd *last_arfile = NULL;
-  char **matching;
-
   format->print_archive_filename (bfd_get_filename (file));
 
   if (print_armap)
     print_symdef_entry (file);
 
+  bfd *last_arfile = NULL;
   for (;;)
     {
-      arfile = bfd_openr_next_archived_file (file, arfile);
-
-      if (arfile == NULL)
+      bfd *arfile = bfd_openr_next_archived_file (file, last_arfile);
+      if (arfile == NULL
+	  || arfile == last_arfile)
 	{
+	  if (arfile != NULL)
+	    bfd_set_error (bfd_error_malformed_archive);
 	  if (bfd_get_error () != bfd_error_no_more_archived_files)
 	    bfd_nonfatal (bfd_get_filename (file));
 	  break;
 	}
 
+      if (last_arfile != NULL)
+	bfd_close (last_arfile);
+
+      char **matching;
       if (bfd_check_format_matches (arfile, bfd_object, &matching))
 	{
 	  set_print_format (arfile);
@@ -1600,12 +1603,6 @@ display_archive (bfd *file)
 	    list_matching_formats (matching);
 	}
 
-      if (last_arfile != NULL)
-	{
-	  bfd_close (last_arfile);
-	  if (arfile == last_arfile)
-	    return;
-	}
       last_arfile = arfile;
     }
 
diff --git a/binutils/objdump.c b/binutils/objdump.c
index 4980929d6ab..f409d679831 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -5888,9 +5888,6 @@ display_any_bfd (bfd *file, int level)
   /* If the file is an archive, process all of its elements.  */
   if (bfd_check_format (file, bfd_archive))
     {
-      bfd *arfile = NULL;
-      bfd *last_arfile = NULL;
-
       if (level == 0)
 	printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
       else if (level > 100)
@@ -5905,30 +5902,25 @@ display_any_bfd (bfd *file, int level)
 	printf (_("In nested archive %s:\n"),
 		sanitize_string (bfd_get_filename (file)));
 
+      bfd *last_arfile = NULL;
       for (;;)
 	{
-	  bfd_set_error (bfd_error_no_error);
-
-	  arfile = bfd_openr_next_archived_file (file, arfile);
-	  if (arfile == NULL)
+	  bfd *arfile = bfd_openr_next_archived_file (file, last_arfile);
+	  if (arfile == NULL
+	      || arfile == last_arfile)
 	    {
+	      if (arfile != NULL)
+		bfd_set_error (bfd_error_malformed_archive);
 	      if (bfd_get_error () != bfd_error_no_more_archived_files)
 		my_bfd_nonfatal (bfd_get_filename (file));
 	      break;
 	    }
 
+	  if (last_arfile != NULL)
+	    bfd_close (last_arfile);
+
 	  display_any_bfd (arfile, level + 1);
 
-	  if (last_arfile != NULL)
-	    {
-	      bfd_close (last_arfile);
-	      /* PR 17512: file: ac585d01.  */
-	      if (arfile == last_arfile)
-		{
-		  last_arfile = NULL;
-		  break;
-		}
-	    }
 	  last_arfile = arfile;
 	}
 
diff --git a/binutils/size.c b/binutils/size.c
index df0ede0c728..ff3db5b57c2 100644
--- a/binutils/size.c
+++ b/binutils/size.c
@@ -368,16 +368,15 @@ display_bfd (bfd *abfd)
 static void
 display_archive (bfd *file)
 {
-  bfd *arfile = (bfd *) NULL;
-  bfd *last_arfile = (bfd *) NULL;
-
+  bfd *last_arfile = NULL;
   for (;;)
     {
-      bfd_set_error (bfd_error_no_error);
-
-      arfile = bfd_openr_next_archived_file (file, arfile);
-      if (arfile == NULL)
+      bfd *arfile = bfd_openr_next_archived_file (file, last_arfile);
+      if (arfile == NULL
+	  || arfile == last_arfile)
 	{
+	  if (arfile != NULL)
+	    bfd_set_error (bfd_error_malformed_archive);
 	  if (bfd_get_error () != bfd_error_no_more_archived_files)
 	    {
 	      bfd_nonfatal (bfd_get_filename (file));
@@ -386,17 +385,10 @@ display_archive (bfd *file)
 	  break;
 	}
 
-      display_bfd (arfile);
-
       if (last_arfile != NULL)
-	{
-	  bfd_close (last_arfile);
-
-	  /* PR 17512: file: a244edbc.  */
-	  if (last_arfile == arfile)
-	    return;
-	}
+	bfd_close (last_arfile);
 
+      display_bfd (arfile);
       last_arfile = arfile;
     }
 

-- 
Alan Modra

                 reply	other threads:[~2024-12-12  5:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z1p1abDJU2uf4opF@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).