[binutils-gdb/binutils-2_46-branch] Fix discarded-qualifiers problems in ldlang.c

H.J. Lu hjl@sourceware.org
Thu May 7 12:26:22 GMT 2026


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

commit 1f5796eedaf99ad287e347f3dd31a75d71fd4402
Author: Calvin Owens <calvin@wbinvd.org>
Date:   Sat May 2 10:03:21 2026 +0930

    Fix discarded-qualifiers problems in ldlang.c
    
    Assign strchr return to a const char* to match its arg in a couple of
    places.  archive_path now returns a const char*, and
    input_statement_is_archive_path now has a const char* sep arg.  If
    you follow where these args come from in ldgram.y it can be seen that
    they are in fact in writable memory, so it isn't necessary to copy
    file_spec to poke in a zero which is restored before the function
    returns.
    
    This partially fixes PR binutils/34125.
    
    Signed-off-by: Calvin Owens <calvin@wbinvd.org>
    Signed-off-by: Alan Modra <amodra@gmail.com>
    (cherry picked from commit 65cd0d66a647f14719e69bd19f34dc98bdb7e635)

Diff:
---
 ld/ldlang.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 253e15c89d4..18aae3a2cad 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -336,10 +336,10 @@ stat_ldirname (const char *name)
 /* If PATTERN is of the form archive:file, return a pointer to the
    separator.  If not, return NULL.  */
 
-static char *
+static const char *
 archive_path (const char *pattern)
 {
-  char *p = NULL;
+  const char *p = NULL;
 
   if (link_info.path_separator == 0)
     return p;
@@ -361,7 +361,7 @@ archive_path (const char *pattern)
    return whether F matches FILE_SPEC.  */
 
 static bool
-input_statement_is_archive_path (const char *file_spec, char *sep,
+input_statement_is_archive_path (const char *file_spec, const char *sep,
 				 lang_input_statement_type *f)
 {
   bool match = false;
@@ -376,9 +376,10 @@ input_statement_is_archive_path (const char *file_spec, char *sep,
       if (sep != file_spec)
 	{
 	  const char *aname = bfd_get_filename (f->the_bfd->my_archive);
-	  *sep = 0;
+	  /* SEP which points into FILE_SPEC is in writable memory.  */
+	  *(char *) sep = 0;
 	  match = name_match (file_spec, aname) == 0;
-	  *sep = link_info.path_separator;
+	  *(char *) sep = link_info.path_separator;
 	}
     }
   return match;
@@ -420,7 +421,7 @@ walk_wild_file_in_exclude_list (struct name_list *exclude_list,
        list_tmp;
        list_tmp = list_tmp->next)
     {
-      char *p = archive_path (list_tmp->name);
+      const char *p = archive_path (list_tmp->name);
 
       if (p != NULL)
 	{
@@ -476,7 +477,7 @@ walk_wild_section_match (lang_wild_statement_type *ptr,
 {
   struct wildcard_list *sec;
   const char *file_spec = ptr->filename;
-  char *p;
+  const char *p;
 
   /* Check if filenames match.  */
   if (file_spec == NULL)
@@ -10741,7 +10742,7 @@ cmdline_fopen_temp (const char *path, const char *target,
 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
   {
     /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
-    char *bslash = strrchr (path, '\\');
+    const char *bslash = strrchr (path, '\\');
 
     if (slash == NULL || (bslash != NULL && bslash > slash))
       slash = bslash;


More information about the Binutils-cvs mailing list