[PATCH v2] perf riscv: Fix discarded const qualifier in _get_field()

Li Guan posted 1 patch 4 weeks, 1 day ago
tools/perf/arch/riscv/util/header.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v2] perf riscv: Fix discarded const qualifier in _get_field()
Posted by Li Guan 4 weeks, 1 day ago
The assignment of strrchr() return values to non-const char * variables
triggers a -Werror=discarded-qualifiers warning when building with GCC 14.
This happens because in newer glibc versions, strrchr() returns a
const char * if the input string is const.

Properly declare 'line2' and 'nl' as const char * to match the glibc
function signature and ensure type safety. This avoids the need for
explicit type casting and aligns with the design pattern of not
modifying read-only memory in the perf tool.

Signed-off-by: Li Guan <guanli.oerv@isrc.iscas.ac.cn>
---
v2:
 - Drop the auxtrace decoupling and weak stub approach as they interfered 
   with the cross-platform analysis intent, per Ian's feedback.
 - Focus on a clean fix for the const qualifier issue in RISC-V header.c
   by properly declaring local variables as const.
 - Use Li Guan as the preferred name format for consistency.
 - Verified that this fix is not yet present in acme/perf-tools-next.

 tools/perf/arch/riscv/util/header.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/header.c
index 4b839203d4..891984e909 100644
--- a/tools/perf/arch/riscv/util/header.c
+++ b/tools/perf/arch/riscv/util/header.c
@@ -19,7 +19,7 @@
 
 static char *_get_field(const char *line)
 {
-	char *line2, *nl;
+	const char *line2, *nl;
 
 	line2 = strrchr(line, ' ');
 	if (!line2)
-- 
2.54.0
Re: [PATCH v2] perf riscv: Fix discarded const qualifier in _get_field()
Posted by Li Guan 2 weeks, 5 days ago
Hi Arnaldo, Namhyung,

Just a gentle ping on this patch. It has received a Reviewed-by from Ian.
Could you please consider picking it up for perf-tools-next?

Thanks,
Li Guan

On 5/14/2026 2:07 AM, Li Guan wrote:
> The assignment of strrchr() return values to non-const char * variables
> triggers a -Werror=discarded-qualifiers warning when building with GCC 14.
> This happens because in newer glibc versions, strrchr() returns a
> const char * if the input string is const.
>
> Properly declare 'line2' and 'nl' as const char * to match the glibc
> function signature and ensure type safety. This avoids the need for
> explicit type casting and aligns with the design pattern of not
> modifying read-only memory in the perf tool.
>
> Signed-off-by: Li Guan <guanli.oerv@isrc.iscas.ac.cn>
> ---
> v2:
>   - Drop the auxtrace decoupling and weak stub approach as they interfered
>     with the cross-platform analysis intent, per Ian's feedback.
>   - Focus on a clean fix for the const qualifier issue in RISC-V header.c
>     by properly declaring local variables as const.
>   - Use Li Guan as the preferred name format for consistency.
>   - Verified that this fix is not yet present in acme/perf-tools-next.
>
>   tools/perf/arch/riscv/util/header.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/header.c
> index 4b839203d4..891984e909 100644
> --- a/tools/perf/arch/riscv/util/header.c
> +++ b/tools/perf/arch/riscv/util/header.c
> @@ -19,7 +19,7 @@
>   
>   static char *_get_field(const char *line)
>   {
> -	char *line2, *nl;
> +	const char *line2, *nl;
>   
>   	line2 = strrchr(line, ' ');
>   	if (!line2)
Re: [PATCH v2] perf riscv: Fix discarded const qualifier in _get_field()
Posted by Arnaldo Carvalho de Melo 2 weeks, 5 days ago
On Sun, May 24, 2026 at 02:41:26AM +0800, Li Guan wrote:
> Hi Arnaldo, Namhyung,
> 
> Just a gentle ping on this patch. It has received a Reviewed-by from Ian.
> Could you please consider picking it up for perf-tools-next?

Thanks, applying.

- Arnaldo

> Thanks,
> Li Guan
> 
> On 5/14/2026 2:07 AM, Li Guan wrote:
> > The assignment of strrchr() return values to non-const char * variables
> > triggers a -Werror=discarded-qualifiers warning when building with GCC 14.
> > This happens because in newer glibc versions, strrchr() returns a
> > const char * if the input string is const.
> > 
> > Properly declare 'line2' and 'nl' as const char * to match the glibc
> > function signature and ensure type safety. This avoids the need for
> > explicit type casting and aligns with the design pattern of not
> > modifying read-only memory in the perf tool.
> > 
> > Signed-off-by: Li Guan <guanli.oerv@isrc.iscas.ac.cn>
> > ---
> > v2:
> >   - Drop the auxtrace decoupling and weak stub approach as they interfered
> >     with the cross-platform analysis intent, per Ian's feedback.
> >   - Focus on a clean fix for the const qualifier issue in RISC-V header.c
> >     by properly declaring local variables as const.
> >   - Use Li Guan as the preferred name format for consistency.
> >   - Verified that this fix is not yet present in acme/perf-tools-next.
> > 
> >   tools/perf/arch/riscv/util/header.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/header.c
> > index 4b839203d4..891984e909 100644
> > --- a/tools/perf/arch/riscv/util/header.c
> > +++ b/tools/perf/arch/riscv/util/header.c
> > @@ -19,7 +19,7 @@
> >   static char *_get_field(const char *line)
> >   {
> > -	char *line2, *nl;
> > +	const char *line2, *nl;
> >   	line2 = strrchr(line, ' ');
> >   	if (!line2)
Re: [PATCH v2] perf riscv: Fix discarded const qualifier in _get_field()
Posted by Ian Rogers 4 weeks, 1 day ago
On Wed, May 13, 2026 at 11:12 AM Li Guan <guanli.oerv@isrc.iscas.ac.cn> wrote:
>
> The assignment of strrchr() return values to non-const char * variables
> triggers a -Werror=discarded-qualifiers warning when building with GCC 14.
> This happens because in newer glibc versions, strrchr() returns a
> const char * if the input string is const.
>
> Properly declare 'line2' and 'nl' as const char * to match the glibc
> function signature and ensure type safety. This avoids the need for
> explicit type casting and aligns with the design pattern of not
> modifying read-only memory in the perf tool.
>
> Signed-off-by: Li Guan <guanli.oerv@isrc.iscas.ac.cn>

Reviewed-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
> v2:
>  - Drop the auxtrace decoupling and weak stub approach as they interfered
>    with the cross-platform analysis intent, per Ian's feedback.
>  - Focus on a clean fix for the const qualifier issue in RISC-V header.c
>    by properly declaring local variables as const.
>  - Use Li Guan as the preferred name format for consistency.
>  - Verified that this fix is not yet present in acme/perf-tools-next.
>
>  tools/perf/arch/riscv/util/header.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/header.c
> index 4b839203d4..891984e909 100644
> --- a/tools/perf/arch/riscv/util/header.c
> +++ b/tools/perf/arch/riscv/util/header.c
> @@ -19,7 +19,7 @@
>
>  static char *_get_field(const char *line)
>  {
> -       char *line2, *nl;
> +       const char *line2, *nl;
>
>         line2 = strrchr(line, ' ');
>         if (!line2)
> --
> 2.54.0
>
>