[1/2] gprof: Fix strchr discarded qualifier call

Message ID 20260502170132.3303884-2-mark@klomp.org
State New
Headers
Series [1/2] gprof: Fix strchr discarded qualifier call |

Commit Message

Mark Wielaard May 2, 2026, 4:56 p.m. UTC
  In annotate_source strrchr is called on a const char *. So the result
should also be stored in a const char *.

	* gprof/source.c (annotate_source): Make name_only a const char *.
---
 gprof/source.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

H.J. Lu May 2, 2026, 10:52 p.m. UTC | #1
On Sun, May 3, 2026 at 1:02 AM Mark Wielaard <mark@klomp.org> wrote:
>
> In annotate_source strrchr is called on a const char *. So the result
> should also be stored in a const char *.
>
>         * gprof/source.c (annotate_source): Make name_only a const char *.
> ---
>  gprof/source.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/gprof/source.c b/gprof/source.c
> index 494f6ecc9b20..aae55f6d758f 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;
>
> --
> 2.54.0
>

We should  combine this strrchr patch set with the patch set from
Calvin Owens <calvin@wbinvd.org>:

https://patchwork.sourceware.org/project/binutils/list/?series=60290
  
H.J. Lu May 3, 2026, 12:32 a.m. UTC | #2
On Sun, May 3, 2026 at 6:52 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Sun, May 3, 2026 at 1:02 AM Mark Wielaard <mark@klomp.org> wrote:
> >
> > In annotate_source strrchr is called on a const char *. So the result
> > should also be stored in a const char *.
> >
> >         * gprof/source.c (annotate_source): Make name_only a const char *.
> > ---
> >  gprof/source.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/gprof/source.c b/gprof/source.c
> > index 494f6ecc9b20..aae55f6d758f 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;
> >
> > --
> > 2.54.0
> >
>
> We should  combine this strrchr patch set with the patch set from
> Calvin Owens <calvin@wbinvd.org>:
>
> https://patchwork.sourceware.org/project/binutils/list/?series=60290
>

Calvin's patch has been checked in.   Here is another one for DOS based
filesystem:

Fix the strrchr error like:

gprof/source.c:130:28: error: initialization discards ‘const’
qualifier from pointer target type [-Werror=discarded-qualifiers]
  130 |             char *bslash = strrchr (sf->name, '\\');

binutils/

* bucomm.c (template_in_dir): Make bslash const char *.

gprof/

* source.c (annotate_source): Make bslash const char *.
  
Alan Modra May 3, 2026, 2:57 a.m. UTC | #3
On Sun, May 03, 2026 at 08:32:51AM +0800, H.J. Lu wrote:
> Calvin's patch has been checked in.   Here is another one for DOS based
> filesystem:

Well, one of them was.

> Fix the strrchr error like:
> 
> gprof/source.c:130:28: error: initialization discards ‘const’
> qualifier from pointer target type [-Werror=discarded-qualifiers]
>   130 |             char *bslash = strrchr (sf->name, '\\');
> 
> binutils/
> 
> * bucomm.c (template_in_dir): Make bslash const char *.
> 
> gprof/
> 
> * source.c (annotate_source): Make bslash const char *.

Please commit.
  
H.J. Lu May 3, 2026, 7:56 p.m. UTC | #4
On Sun, May 3, 2026 at 1:02 AM Mark Wielaard <mark@klomp.org> wrote:
>
> In annotate_source strrchr is called on a const char *. So the result
> should also be stored in a const char *.
>
>         * gprof/source.c (annotate_source): Make name_only a const char *.
> ---
>  gprof/source.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/gprof/source.c b/gprof/source.c
> index 494f6ecc9b20..aae55f6d758f 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;
>
> --
> 2.54.0
>

I think this counts an obvious fix.
  

Patch

diff --git a/gprof/source.c b/gprof/source.c
index 494f6ecc9b20..aae55f6d758f 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;