[4/4] ld: Fix discarded-qualifiers problems in ldelf.c

Message ID 3197a710e4b6bd87166a0dde13812259d09b452f.1777074100.git.calvin@wbinvd.org
State New
Headers
Series Fix build errors due to qualifier preserving strchr() |

Commit Message

Calvin Owens April 25, 2026, 2:19 a.m. UTC
  ../../ld/ldelf.c: In function ‘ldelf_search_needed’:
    ../../ld/ldelf.c:538:34: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
      538 |                       if ((slash = strrchr (replacement, '/')) != NULL)
          |                                  ^

Fix this by passing the obviously equal non-const pointer to strrchr(),
instead of the const one.

    ../../ld/ldelf.c: In function ‘ldelf_parse_ld_so_conf_include’:
    ../../ld/ldelf.c:797:17: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
      797 |       char *p = strrchr (filename, '/');
          |                 ^~~~~~~

Fix this by declaring the local pointer varaible as const, since it is
not modified.

Signed-off-by: Calvin Owens <calvin@wbinvd.org>
---
 ld/ldelf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Mark Wielaard May 3, 2026, 12:34 p.m. UTC | #1
Hi Calvin,

On Fri, Apr 24, 2026 at 07:19:47PM -0700, Calvin Owens wrote:
>     ../../ld/ldelf.c: In function ‘ldelf_search_needed’:
>     ../../ld/ldelf.c:538:34: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>       538 |                       if ((slash = strrchr (replacement, '/')) != NULL)
>           |                                  ^
> 
> Fix this by passing the obviously equal non-const pointer to strrchr(),
> instead of the const one.

This one I also found and fixed in almost the same way in
https://inbox.sourceware.org/binutils/20260502170132.3303884-3-mark@klomp.org/

> 
>     ../../ld/ldelf.c: In function ‘ldelf_parse_ld_so_conf_include’:
>     ../../ld/ldelf.c:797:17: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>       797 |       char *p = strrchr (filename, '/');
>           |                 ^~~~~~~
> 
> Fix this by declaring the local pointer varaible as const, since it is
> not modified.

Likewise.

Again my apologies for not seeing your patch earlier. It would have
saved me some unnecessary work.

Cheers,

Mark
  
Calvin Owens May 3, 2026, 7:56 p.m. UTC | #2
On Sunday 05/03 at 14:34 +0200, Mark Wielaard wrote:
> Hi Calvin,
> 
> On Fri, Apr 24, 2026 at 07:19:47PM -0700, Calvin Owens wrote:
> >     ../../ld/ldelf.c: In function ‘ldelf_search_needed’:
> >     ../../ld/ldelf.c:538:34: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
> >       538 |                       if ((slash = strrchr (replacement, '/')) != NULL)
> >           |                                  ^
> > 
> > Fix this by passing the obviously equal non-const pointer to strrchr(),
> > instead of the const one.
> 
> This one I also found and fixed in almost the same way in
> https://inbox.sourceware.org/binutils/20260502170132.3303884-3-mark@klomp.org/
> 
> > 
> >     ../../ld/ldelf.c: In function ‘ldelf_parse_ld_so_conf_include’:
> >     ../../ld/ldelf.c:797:17: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
> >       797 |       char *p = strrchr (filename, '/');
> >           |                 ^~~~~~~
> > 
> > Fix this by declaring the local pointer varaible as const, since it is
> > not modified.
> 
> Likewise.
> 
> Again my apologies for not seeing your patch earlier. It would have
> saved me some unnecessary work.

No problem at all. It's really simple stuff, I had spun them for some
local testing and just thought they might save you all some time. Please
don't go to any extra trouble to use them :)

Thanks,
Calvin

> Cheers,
> 
> Mark
  

Patch

diff --git a/ld/ldelf.c b/ld/ldelf.c
index 15d4b576bc0..518ea45397b 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -535,7 +535,7 @@  ldelf_search_needed (const char *path, struct dt_needed *n, int force,
 			}
 
 		      replacement = freeme;
-		      if ((slash = strrchr (replacement, '/')) != NULL)
+		      if ((slash = strrchr (freeme, '/')) != NULL)
 			* slash = 0;
 		    }
 		}
@@ -794,7 +794,7 @@  ldelf_parse_ld_so_conf_include (struct ldelf_ld_so_conf *info,
 
   if (pattern[0] != '/')
     {
-      char *p = strrchr (filename, '/');
+      const char *p = strrchr (filename, '/');
       size_t patlen = strlen (pattern) + 1;
 
       newp = xmalloc (p - filename + 1 + patlen);