[PATCH 2/3] perf riscv: Fix discarded const qualifier error in _get_field()

Ian Rogers irogers at google.com
Wed May 13 09:18:03 PDT 2026


On Wed, May 13, 2026 at 8:50 AM Li Guan <guanli.oerv at isrc.iscas.ac.cn> wrote:
>
> When building perf for the RISC-V architecture (e.g., with GCC 14), the
> build fails due to strict type checking on pointer assignments. The
> compiler flags the return value of strrchr() being assigned to a
> non-const pointer while processing a const string, triggering
> -Werror=discarded-qualifiers:
>
>     arch/riscv/util/header.c:24:15: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>        24 |          line2 = strrchr(line, ' ');
>           |                ^
>     arch/riscv/util/header.c:29:12: error: assignment discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>        29 |          nl = strrchr(line, '\n');
>           |             ^
>
> Resolve this by adding an explicit (char *) cast to the strrchr()
> return values, which satisfies the compiler's qualifier checks
> without altering the runtime behavior.

Why not add the const? There have been a few rounds of this clean up, like:
https://lore.kernel.org/linux-perf-users/20251211221756.96294-3-acme@kernel.org/
Did you check it wasn't already fixed in perf-tools-next?

Thanks,
Ian

> Signed-off-by: Li Guan <guanli.oerv at isrc.iscas.ac.cn>
> ---
>  tools/perf/arch/riscv/util/header.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/header.c
> index 4b839203d4..d01ba64aec 100644
> --- a/tools/perf/arch/riscv/util/header.c
> +++ b/tools/perf/arch/riscv/util/header.c
> @@ -21,12 +21,12 @@ static char *_get_field(const char *line)
>  {
>         char *line2, *nl;
>
> -       line2 = strrchr(line, ' ');
> +       line2 = (char *)strrchr(line, ' ');
>         if (!line2)
>                 return NULL;
>
>         line2++;
> -       nl = strrchr(line, '\n');
> +       nl = (char *)strrchr(line, '\n');
>         if (!nl)
>                 return NULL;
>
> --
> 2.54.0
>



More information about the linux-riscv mailing list