public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: PR31191, objcopy leaves temporary files
Date: Wed, 27 Dec 2023 16:35:19 +1030	[thread overview]
Message-ID: <ZYu+n7HsEnQSju5J@squeak.grove.modra.org> (raw)

Fix the ENOTDIR rmdir too.

	PR 31191
	* objcopy.c (copy_archive): Localise uses of "l".  Remove
	const from name_list.name.  Unlink output element on bfd_close
	error, and NULL list->name to indicate file is removed.  Adjust
	cleanup to prevent rmdir on non-existent file.

diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index b3c8d0f81ac..0c4a9e6407f 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3610,9 +3610,9 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
   struct name_list
     {
       struct name_list *next;
-      const char *name;
+      char *name;
       bfd *obfd;
-    } *list, *l;
+    } *list;
   bfd **ptr = &obfd->archive_head;
   bfd *this_element;
   char *dir = NULL;
@@ -3671,7 +3671,6 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
       bfd *output_element;
       struct stat buf;
       int stat_status = 0;
-      bool del = true;
       bool ok_object;
 
       /* PR binutils/17533: Do not allow directory traversal
@@ -3704,7 +3703,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
 	      goto cleanup_and_exit;
 	    }
 
-	  l = (struct name_list *) xmalloc (sizeof (struct name_list));
+	  struct name_list *l = xmalloc (sizeof (*l));
 	  l->name = tmpdir;
 	  l->next = list;
 	  l->obfd = NULL;
@@ -3723,7 +3722,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
 		       bfd_get_filename (this_element));
 	}
 
-      l = (struct name_list *) xmalloc (sizeof (struct name_list));
+      struct name_list *l = xmalloc (sizeof (*l));
       l->name = output_name;
       l->next = list;
       l->obfd = NULL;
@@ -3751,32 +3750,31 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
 
       if (ok_object)
 	{
-	  del = !copy_object (this_element, output_element, input_arch);
+	  status = !copy_object (this_element, output_element, input_arch);
 
-	  if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
+	  if (status && bfd_get_arch (this_element) == bfd_arch_unknown)
 	    /* Try again as an unknown object file.  */
 	    ok_object = false;
 	}
 
       if (!ok_object)
-	del = !copy_unknown_object (this_element, output_element);
+	status = !copy_unknown_object (this_element, output_element);
 
-      if (!(ok_object && !del && !status
+      if (!(ok_object && !status
 	    ? 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.  */
+	  /* Error in new object file.  Don't change archive.  */
 	  status = 1;
 	}
 
-      if (del)
+      if (status)
 	{
 	  unlink (output_name);
-	  status = 1;
+	  free (output_name);
+	  list->name = NULL;
+	  bfd_close (this_element);
 	}
-
-      if (status)
-	bfd_close (this_element);
       else
 	{
 	  if (preserve_dates && stat_status == 0)
@@ -3785,7 +3783,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
 	  /* Open the newly created output file and attach to our list.  */
 	  output_element = bfd_openr (output_name, output_target);
 
-	  l->obfd = output_element;
+	  list->obfd = output_element;
 
 	  *ptr = output_element;
 	  ptr = &output_element->archive_next;
@@ -3817,17 +3815,20 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
   free (filename);
 
   /* Delete all the files that we opened.  */
-  struct name_list *next;
+  struct name_list *l, *next;
   for (l = list; l != NULL; l = next)
     {
-      if (l->obfd == NULL)
-	rmdir (l->name);
-      else
+      if (l->name != NULL)
 	{
-	  bfd_close (l->obfd);
-	  unlink (l->name);
+	  if (l->obfd == NULL)
+	    rmdir (l->name);
+	  else
+	    {
+	      bfd_close (l->obfd);
+	      unlink (l->name);
+	    }
+	  free (l->name);
 	}
-      free ((char *) l->name);
       next = l->next;
       free (l);
     }

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2023-12-27  6:05 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=ZYu+n7HsEnQSju5J@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).