[binutils-gdb] PR 27666, bfd_check_format_matches

Alan Modra amodra@sourceware.org
Sat Dec 20 04:53:32 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=12b471a0007bf9b69fcdd2a2e085cf8d07157133

commit 12b471a0007bf9b69fcdd2a2e085cf8d07157133
Author: Alan Modra <amodra@gmail.com>
Date:   Sat Dec 20 14:05:57 2025 +1030

    PR 27666, bfd_check_format_matches
    
    bfd_check_format_matches should not change what matches depending on
    its "matching" arg.  Fix that.  This bug in bfd_check_format_matches
    is the underlying cause of pr27666.
    
            PR 27666
            * format.c (bfd_check_format_matches): Always allocate
            matching_vector.

Diff:
---
 bfd/format.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/bfd/format.c b/bfd/format.c
index 81f29c5f54f..bc05039dd9b 100644
--- a/bfd/format.c
+++ b/bfd/format.c
@@ -446,7 +446,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 {
   extern const bfd_target binary_vec;
   const bfd_target * const *target;
-  const bfd_target **matching_vector = NULL;
+  const bfd_target **matching_vector;
   const bfd_target *save_targ, *right_targ, *ar_right_targ, *match_targ;
   const bfd_target *fail_targ;
   int match_count, best_count, best_match;
@@ -471,15 +471,10 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
   if (abfd->format != bfd_unknown)
     return abfd->format == format;
 
-  if (matching != NULL || *bfd_associated_vector != NULL)
-    {
-      size_t amt;
-
-      amt = sizeof (*matching_vector) * 2 * _bfd_target_vector_entries;
-      matching_vector = (const bfd_target **) bfd_malloc (amt);
-      if (!matching_vector)
-	return false;
-    }
+  matching_vector = bfd_malloc (sizeof (*matching_vector)
+				* 2 * _bfd_target_vector_entries);
+  if (!matching_vector)
+    return false;
 
   /* Avoid clashes with bfd_cache_close_all running in another
      thread.  */
@@ -643,8 +638,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 	      if (abfd->xvec == bfd_default_vector[0])
 		goto ok_ret;
 
-	      if (matching_vector)
-		matching_vector[match_count] = abfd->xvec;
+	      matching_vector[match_count] = abfd->xvec;
 	      match_count++;
 
 	      if (match_priority < best_match)
@@ -666,8 +660,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 		 better matches.  */
 	      if (ar_right_targ != bfd_default_vector[0])
 		ar_right_targ = *target;
-	      if (matching_vector)
-		matching_vector[ar_match_index] = *target;
+	      matching_vector[ar_match_index] = *target;
 	      ar_match_index++;
 	    }
 
@@ -697,7 +690,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
 	{
 	  match_count = ar_match_index - _bfd_target_vector_entries;
 
-	  if (matching_vector && match_count > 1)
+	  if (match_count > 1)
 	    memcpy (matching_vector,
 		    matching_vector + _bfd_target_vector_entries,
 		    sizeof (*matching_vector) * match_count);
@@ -731,7 +724,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching)
   /* We still have more than one equally good match, and at least some
      of the targets support match priority.  Choose the first of the
      best matches.  */
-  if (matching_vector && match_count > 1 && best_count != match_count)
+  if (match_count > 1 && best_count != match_count)
     {
       int i;


More information about the Binutils-cvs mailing list