Always free matching vector from bfd_check_format_matches
authorAlan Modra <amodra@gmail.com>
Thu, 16 Jun 2022 05:18:13 +0000 (14:48 +0930)
committerAlan Modra <amodra@gmail.com>
Thu, 16 Jun 2022 05:31:14 +0000 (15:01 +0930)
At least one place calling list_matching_formats failed to free the
"matching" vector from bfd_check_format_matches afterwards.  Fix that
by calling free inside list_matching_formats.

binutils/
* bucomm.c (list_matching_formats): Free arg.
* addr2line.c (process_file): Adjust to suit.
* ar.c (open_inarch, ranlib_touch): Likewise.
* coffdump.c (main): Likewise.
* nm.c (display_archive, display_file): Likewise.
* objcopy.c (copy_file): Likewise.
* objdump.c (display_object_bfd): Likewise.
* size.c (display_bfd): Likewise.
* srconv.c (main): Likewise.
ld/
* ldlang.c (load_symbols): Free "matching".

binutils/addr2line.c
binutils/ar.c
binutils/bucomm.c
binutils/coffdump.c
binutils/nm.c
binutils/objcopy.c
binutils/objdump.c
binutils/size.c
binutils/srconv.c
ld/ldlang.c

index fd2ac80dacd2fd14b353a7acf55aeb6f8d826288..baf16716182802e6e21c9d771f624fbb549c1012 100644 (file)
@@ -452,10 +452,7 @@ process_file (const char *file_name, const char *section_name,
     {
       bfd_nonfatal (bfd_get_filename (abfd));
       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (matching);
-         free (matching);
-       }
+       list_matching_formats (matching);
       xexit (1);
     }
 
index 6f3f1d6946fe09660d6cd45a58a79d2cecd6cc06..0fdf0679b1c4325d1363d3c342cc0f96176f2082 100644 (file)
@@ -1037,10 +1037,7 @@ open_inarch (const char *archive_filename, const char *file)
     {
       bfd_nonfatal (archive_filename);
       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (matching);
-         free (matching);
-       }
+       list_matching_formats (matching);
       xexit (1);
     }
 
@@ -1615,10 +1612,7 @@ ranlib_touch (const char *archname)
     {
       bfd_nonfatal (archname);
       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (matching);
-         free (matching);
-       }
+       list_matching_formats (matching);
       xexit (1);
     }
 
index 5cf9de75ff7dda375094f2c0be2860b373120547..fdc2209df9c65abb335fb5d2f842e850840959bc 100644 (file)
@@ -160,15 +160,17 @@ set_default_bfd_target (void)
 
 /* After a FALSE return from bfd_check_format_matches with
    bfd_get_error () == bfd_error_file_ambiguously_recognized, print
-   the possible matching targets.  */
+   the possible matching targets and free the list of targets.  */
 
 void
-list_matching_formats (char **p)
+list_matching_formats (char **matching)
 {
   fflush (stdout);
   fprintf (stderr, _("%s: Matching formats:"), program_name);
+  char **p = matching;
   while (*p)
     fprintf (stderr, " %s", *p++);
+  free (matching);
   fputc ('\n', stderr);
 }
 
index d760a6ccb3989fa1f822641f132bb0aa3c92841a..bccc395e5f881f1878a82d892c9dfad25d5272f1 100644 (file)
@@ -541,10 +541,7 @@ main (int ac, char **av)
       bfd_nonfatal (input_file);
 
       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (matching);
-         free (matching);
-       }
+       list_matching_formats (matching);
       exit (1);
     }
 
index 539c5688425a9c151beabe5249d2e5eae2fa1fff..49726eeb83bc6ee2987408f81dbdf9204de6ea81 100644 (file)
@@ -1626,10 +1626,7 @@ display_archive (bfd *file)
        {
          bfd_nonfatal (bfd_get_filename (arfile));
          if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-           {
-             list_matching_formats (matching);
-             free (matching);
-           }
+           list_matching_formats (matching);
        }
 
       if (last_arfile != NULL)
@@ -1686,10 +1683,7 @@ display_file (char *filename)
     {
       bfd_nonfatal (filename);
       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (matching);
-         free (matching);
-       }
+       list_matching_formats (matching);
       retval = false;
     }
 
index 7ad39e854f01f98ae8bc2c14b4e77eccaef30be2..df87712df981520a0802017fbb94a1b5d1e59946 100644 (file)
@@ -3925,15 +3925,9 @@ copy_file (const char *input_filename, const char *output_filename, int ofd,
       bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
 
       if (obj_error == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (obj_matching);
-         free (obj_matching);
-       }
+       list_matching_formats (obj_matching);
       if (core_error == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (core_matching);
-         free (core_matching);
-       }
+       list_matching_formats (core_matching);
 
       status = 1;
     }
index e8fa8ca154e943d3ebab249234cbdc50a8780532..678240535271a218840fe93075fec0b9a0e97088 100644 (file)
@@ -5426,7 +5426,6 @@ display_object_bfd (bfd *abfd)
     {
       nonfatal (bfd_get_filename (abfd));
       list_matching_formats (matching);
-      free (matching);
       return;
     }
 
@@ -5445,10 +5444,7 @@ display_object_bfd (bfd *abfd)
   nonfatal (bfd_get_filename (abfd));
 
   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-    {
-      list_matching_formats (matching);
-      free (matching);
-    }
+    list_matching_formats (matching);
 }
 
 static void
index 86dc13e47401f248ae6d90723d0ead644419da78..710402537e5ab64ce380b6af8fa964a8606ae30e 100644 (file)
@@ -337,7 +337,6 @@ display_bfd (bfd *abfd)
     {
       bfd_nonfatal (bfd_get_filename (abfd));
       list_matching_formats (matching);
-      free (matching);
       return_code = 3;
       return;
     }
@@ -360,10 +359,7 @@ display_bfd (bfd *abfd)
   bfd_nonfatal (bfd_get_filename (abfd));
 
   if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-    {
-      list_matching_formats (matching);
-      free (matching);
-    }
+    list_matching_formats (matching);
 
   return_code = 3;
 }
index 6fc98bc49d53a55ed8a34ac589a8dcbf4e27dca4..d017edff78866a2d811c3b13694f3ad1094b1acf 100644 (file)
@@ -1828,10 +1828,7 @@ main (int ac, char **av)
       bfd_nonfatal (input_file);
 
       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
-       {
-         list_matching_formats (matching);
-         free (matching);
-       }
+       list_matching_formats (matching);
       exit (1);
     }
 
index f10557af8428ccbca83a57de9ca315f8afff5660..839535bdb7342b3f9b18c7811ce442837cbe6514 100644 (file)
@@ -3056,7 +3056,11 @@ load_symbols (lang_input_statement_type *entry,
 
       /* See if the emulation has some special knowledge.  */
       if (ldemul_unrecognized_file (entry))
-       return true;
+       {
+         if (err == bfd_error_file_ambiguously_recognized)
+           free (matching);
+         return true;
+       }
 
       if (err == bfd_error_file_ambiguously_recognized)
        {
@@ -3066,6 +3070,7 @@ load_symbols (lang_input_statement_type *entry,
                   " matching formats:"), entry->the_bfd);
          for (p = matching; *p != NULL; p++)
            einfo (" %s", *p);
+         free (matching);
          einfo ("%F\n");
        }
       else if (err != bfd_error_file_not_recognized