[PATCH v3 14/18] LD: Guard `new_afile' result with assertions

Maciej W. Rozycki macro@orcam.me.uk
Thu Nov 20 10:56:53 GMT 2025


From: Maciej W. Rozycki <macro@redhat.com>

Calls to `new_afile' can in principle return a NULL pointer.  However 
in `lookup_name' we dereference the pointer returned without validating 
it first.  Since it's not supposed to be NULL there except where we have 
hit an internal consistency issue just add an assertion for meaningful 
output rather than just a segfault.

For consistency update `cmdline_load_object_only_section' replacing a 
call to `abort' with the same assertion.
---
No change from v2 (12/16), 
<https://inbox.sourceware.org/binutils/alpine.DEB.2.21.2511061740420.25436@angie.orcam.me.uk/>.

New change in v2.
---
 ld/ldlang.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

binutils-ld-lookup-name-assert.diff
Index: binutils-gdb/ld/ldlang.c
===================================================================
--- binutils-gdb.orig/ld/ldlang.c
+++ binutils-gdb/ld/ldlang.c
@@ -3017,6 +3017,7 @@ lookup_name (const char *name)
       *stat_ptr->tail = rest;
       if (*tail == NULL)
 	stat_ptr->tail = tail;
+      ASSERT (search != NULL);
     }
 
   /* If we have already added this file, or this file is not real
@@ -11063,8 +11064,7 @@ cmdline_load_object_only_section (const
   lang_input_statement_type *entry
     = new_afile (name, lang_input_file_is_file_enum, NULL, NULL);
 
-  if (!entry)
-    abort ();
+  ASSERT (entry != NULL);
 
   ldfile_open_file (entry);
 


More information about the Binutils mailing list