[1/4] gprof: Fix discarded-qualifiers problems in source.c

Message ID 003fed7e07f6f9ed6a8c365e31325162a840fa03.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
  ../../gprof/source.c: In function ‘annotate_source’:
    ../../gprof/source.c:126:21: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
      126 |           name_only = strrchr (sf->name, '/');
          |                     ^

Fix this and the similar problem immediately below it by declaring the
local pointer varaibles as const, since they are not modified.

Signed-off-by: Calvin Owens <calvin@wbinvd.org>
---
 gprof/source.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
  

Comments

Simon Marchi April 26, 2026, 2:35 a.m. UTC | #1
On 2026-04-24 22:19, Calvin Owens wrote:
>     ../../gprof/source.c: In function ‘annotate_source’:
>     ../../gprof/source.c:126:21: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>       126 |           name_only = strrchr (sf->name, '/');
>           |                     ^
> 
> Fix this and the similar problem immediately below it by declaring the
> local pointer varaibles as const, since they are not modified.
> 
> Signed-off-by: Calvin Owens <calvin@wbinvd.org>
> ---
>  gprof/source.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/gprof/source.c b/gprof/source.c
> index 494f6ecc9b2..322d1fe25a7 100644
> --- a/gprof/source.c
> +++ b/gprof/source.c
> @@ -98,7 +98,8 @@ annotate_source (Source_File *sf, unsigned int max_width,
>    bool new_line;
>    char buf[8192];
>    char *fname;
> -  char *annotation, *name_only;
> +  char *annotation;
> +  const char *name_only;
>    FILE *ifp, *ofp;
>    Search_List_Elem *sle = src_search_list.head;
>  
> @@ -126,11 +127,11 @@ annotate_source (Source_File *sf, unsigned int max_width,
>  	  name_only = strrchr (sf->name, '/');
>  #ifdef HAVE_DOS_BASED_FILE_SYSTEM
>  	  {
> -	    char *bslash = strrchr (sf->name, '\\');
> +	    const char *bslash = strrchr (sf->name, '\\');
>  	    if (name_only == NULL || (bslash != NULL && bslash > name_only))
>  	      name_only = bslash;
>  	    if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
> -	      name_only = (char *)sf->name + 1;
> +	      name_only = (const char *)sf->name + 1;

I think that you can just remove this cast to `const char *`, since
`sf->name` is already a `const char *`.

There is another spot to change, variable `bslash` at line 187 of the
same file.  I found this by inverting all #if conditions just to
build-test.

Simon
  
Mark Wielaard May 3, 2026, 12:25 p.m. UTC | #2
Hi Calvin,

On Sat, Apr 25, 2026 at 10:35:04PM -0400, Simon Marchi wrote:
> 
> 
> On 2026-04-24 22:19, Calvin Owens wrote:
> >     ../../gprof/source.c: In function ‘annotate_source’:
> >     ../../gprof/source.c:126:21: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
> >       126 |           name_only = strrchr (sf->name, '/');
> >           |                     ^
> > 
> > Fix this and the similar problem immediately below it by declaring the
> > local pointer varaibles as const, since they are not modified.
> > 
> > Signed-off-by: Calvin Owens <calvin@wbinvd.org>
> > ---
> >  gprof/source.c | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> > 
> > diff --git a/gprof/source.c b/gprof/source.c
> > index 494f6ecc9b2..322d1fe25a7 100644
> > --- a/gprof/source.c
> > +++ b/gprof/source.c
> > @@ -98,7 +98,8 @@ annotate_source (Source_File *sf, unsigned int max_width,
> >    bool new_line;
> >    char buf[8192];
> >    char *fname;
> > -  char *annotation, *name_only;
> > +  char *annotation;
> > +  const char *name_only;
> >    FILE *ifp, *ofp;
> >    Search_List_Elem *sle = src_search_list.head;

Sorry, I didn't see you already proposed a fix for this.
I think this part is correct and I reinvented it here:
https://inbox.sourceware.org/binutils/20260502170132.3303884-2-mark@klomp.org/

> > @@ -126,11 +127,11 @@ annotate_source (Source_File *sf, unsigned int max_width,
> >  	  name_only = strrchr (sf->name, '/');
> >  #ifdef HAVE_DOS_BASED_FILE_SYSTEM
> >  	  {
> > -	    char *bslash = strrchr (sf->name, '\\');
> > +	    const char *bslash = strrchr (sf->name, '\\');
> >  	    if (name_only == NULL || (bslash != NULL && bslash > name_only))
> >  	      name_only = bslash;
> >  	    if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
> > -	      name_only = (char *)sf->name + 1;
> > +	      name_only = (const char *)sf->name + 1;
> 
> I think that you can just remove this cast to `const char *`, since
> `sf->name` is already a `const char *`.
> 
> There is another spot to change, variable `bslash` at line 187 of the
> same file.  I found this by inverting all #if conditions just to
> build-test.

I believe H.J. fixed this here:

https://inbox.sourceware.org/binutils/CAMe9rOpJsbA5QZssE9ejG5WCuw5hU+sQyvgcMSnDjqH-Yv22XQ@mail.gmail.com/

But the first part should still go in to fix the build with GCC 16.1
and glibc 2.43.

Cheers,

Mark
  

Patch

diff --git a/gprof/source.c b/gprof/source.c
index 494f6ecc9b2..322d1fe25a7 100644
--- a/gprof/source.c
+++ b/gprof/source.c
@@ -98,7 +98,8 @@  annotate_source (Source_File *sf, unsigned int max_width,
   bool new_line;
   char buf[8192];
   char *fname;
-  char *annotation, *name_only;
+  char *annotation;
+  const char *name_only;
   FILE *ifp, *ofp;
   Search_List_Elem *sle = src_search_list.head;
 
@@ -126,11 +127,11 @@  annotate_source (Source_File *sf, unsigned int max_width,
 	  name_only = strrchr (sf->name, '/');
 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
 	  {
-	    char *bslash = strrchr (sf->name, '\\');
+	    const char *bslash = strrchr (sf->name, '\\');
 	    if (name_only == NULL || (bslash != NULL && bslash > name_only))
 	      name_only = bslash;
 	    if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
-	      name_only = (char *)sf->name + 1;
+	      name_only = (const char *)sf->name + 1;
 	  }
 #endif
 	  if (name_only)