]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Don't access freed memory printing objcopy warning
authorAlan Modra <amodra@gmail.com>
Wed, 14 Dec 2022 01:12:00 +0000 (11:42 +1030)
committerAlan Modra <amodra@gmail.com>
Wed, 14 Dec 2022 02:37:43 +0000 (13:07 +1030)
abfd->filename will be freed if bfd_close gets far enough to delete
the bfd.  It's possible to have an error from fclose at this point.

* objcopy.c (copy_archive): Dup filename before closing bfd for
potential use in bfd_nonfatal_message.

binutils/objcopy.c

index be08f7bd0f957c363af237594fb641bca9274a75..289b62355af775a033328e3c1aa9158d8cfa1bb6 100644 (file)
@@ -3543,7 +3543,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
   bfd **ptr = &obfd->archive_head;
   bfd *this_element;
   char *dir;
-  const char *filename;
+  char *filename;
 
   /* PR 24281: It is not clear what should happen when copying a thin archive.
      One part is straight forward - if the output archive is in a different
@@ -3720,19 +3720,21 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
     }
   *ptr = NULL;
 
-  filename = bfd_get_filename (obfd);
+  filename = xstrdup (bfd_get_filename (obfd));
   if (!(status == 0 ? bfd_close : bfd_close_all_done) (obfd))
     {
       status = 1;
       bfd_nonfatal_message (filename, NULL, NULL, NULL);
     }
+  free (filename);
 
-  filename = bfd_get_filename (ibfd);
+  filename = xstrdup (bfd_get_filename (ibfd));
   if (!bfd_close (ibfd))
     {
       status = 1;
       bfd_nonfatal_message (filename, NULL, NULL, NULL);
     }
+  free (filename);
 
  cleanup_and_exit:
   /* Delete all the files that we opened.  */