[binutils-gdb/binutils-2_46-branch] binutils: fix C23 -Wdiscarded-qualifiers errors

"H.J. Lu via Binutils-cvs" <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=922d530f0aa6973b92489e4c84a449bf09421b1f

commit 922d530f0aa6973b92489e4c84a449bf09421b1f
Author: Simon Marchi <[email protected]>
Date:   Fri Feb 13 14:17:40 2026 -0500

    binutils: fix C23 -Wdiscarded-qualifiers errors
    
    When compiling with gcc 15, I get these:
    
          CC       prdbg.o
        /home/simark/src/binutils-gdb/binutils/prdbg.c: In function ‘tg_start_function’:
        /home/simark/src/binutils-gdb/binutils/prdbg.c:2658:11: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
         2658 |       sep = strchr (name, '(');
              |           ^
        cc1: all warnings being treated as errors
        make[2]: *** [Makefile:1220: prdbg.o] Error 1
          CC       addr2line.o
        /home/simark/src/binutils-gdb/binutils/addr2line.c: In function ‘translate_addresses’:
        /home/simark/src/binutils-gdb/binutils/addr2line.c:390:21: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
          390 |                   h = strrchr (filename, '/');
              |                     ^
        cc1: all warnings being treated as errors
        make[2]: *** [Makefile:1220: addr2line.o] Error 1
          CC       nm.o
        /home/simark/src/binutils-gdb/binutils/nm.c: In function ‘print_symname’:
        /home/simark/src/binutils-gdb/binutils/nm.c:689:21: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
          689 |       char *atver = strchr (name, '@');
              |                     ^~~~~~
        cc1: all warnings being treated as errors
    
    I think they are related to the fact that strchr having changed to
    return const when passed a const.
    
    This partially fixes PR binutils/34125.
    
    Change-Id: Idb829916378f0a18e717d1e80414aaa5eba4f7f0
    (cherry picked from commit 9cb12adfb54bc3dabf4ee00296064245eb7660ef)

Diff:
---
 binutils/addr2line.c | 2 +-
 binutils/nm.c        | 2 +-
 binutils/prdbg.c     | 8 +++++---
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/binutils/addr2line.c b/binutils/addr2line.c
index 141a7099de4..f8c110f532f 100644
--- a/binutils/addr2line.c
+++ b/binutils/addr2line.c
@@ -385,7 +385,7 @@ translate_addresses (bfd *abfd, asection *section)
 
               if (base_names && filename != NULL)
                 {
-                  char *h;
+                  const char *h;
 
                   h = strrchr (filename, '/');
                   if (h != NULL)
diff --git a/binutils/nm.c b/binutils/nm.c
index 3aef8ebe098..c5f7e68e9df 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -686,7 +686,7 @@ print_symname (const char *form, struct extended_symbol_info *info,
   if (!with_symbol_versions
       && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
     {
-      char *atver = strchr (name, '@');
+      const char *atver = strchr (name, '@');
 
       if (atver)
 	{
diff --git a/binutils/prdbg.c b/binutils/prdbg.c
index 287083df1b9..ff24eac54fb 100644
--- a/binutils/prdbg.c
+++ b/binutils/prdbg.c
@@ -2642,22 +2642,24 @@ tg_start_function (void *p, const char *name, bool global)
   if (dname != NULL)
     {
       char *sep;
+      char *mutable_name;
       sep = strstr (dname, "::");
       if (sep)
 	{
 	  info->stack->method = dname;
 	  dname = NULL;
 	  *sep = 0;
-	  name = sep + 2;
+	  mutable_name = sep + 2;
 	}
       else
 	{
 	  info->stack->method = xstrdup ("");
-	  name = dname;
+	  mutable_name = dname;
 	}
-      sep = strchr (name, '(');
+      sep = strchr (mutable_name, '(');
       if (sep)
 	*sep = 0;
+      name = mutable_name;
       /* Obscure functions as type_info function.  */
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.