Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Li Guan" <guanli.oerv@isrc.iscas.ac.cn>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 2/3] perf riscv: Fix discarded const qualifier error in _get_field()
Date: Thu, 14 May 2026 07:38:25 +0000	[thread overview]
Message-ID: <20260514073825.95CE8C2BCC6@smtp.kernel.org> (raw)
In-Reply-To: <20260513154851.1085334-3-guanli.oerv@isrc.iscas.ac.cn>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Deterministic NULL pointer dereference in `get_cpuid()` when `/proc/cpuinfo` parsing fails.
- [Low] The patch resolves a const-qualifier warning by forcefully casting to `(char *)` instead of using const-correct variable types.
--

commit 5d7d955a53ac900eccabdb8ae292dcf199883310
Author: Li Guan <guanli.oerv@isrc.iscas.ac.cn>

perf riscv: Fix discarded const qualifier error in _get_field()

This commit addresses a build failure on GCC 14 caused by strict type checking
on pointer assignments. It resolves the discarded-qualifiers warning by
explicitly casting the return values of strrchr() to a (char *).

> diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/header.c
> index 4b839203d4a54..d01ba64aecfd5 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, ' ');

Would it be better to declare line2 and nl as const char * rather than
forcefully casting away the const qualifier?

Since these variables are only used for pointer arithmetic and passed to
strndup() (which accepts a const char *), propagating the const qualifier
would prevent bypassing the compiler safety checks.

>  	if (!line2)
>  		return NULL;
>  
>  	line2++;
> -	nl = strrchr(line, '\n');
> +	nl = (char *)strrchr(line, '\n');
>  	if (!nl)
>  		return NULL;

While reviewing this file, I also noticed a pre-existing issue in the
get_cpuid() function:

tools/perf/arch/riscv/util/header.c:get_cpuid() {
	char *cpuid = _get_cpuid();
	int ret = 0;

	if (sz < strlen(cpuid)) {
		ret = -EINVAL;
		goto free;
	}
...
}

Can this unconditional call to strlen() cause a segmentation fault if
_get_cpuid() returns NULL?

If _get_cpuid() fails to open /proc/cpuinfo or cannot find the expected
fields (mvendorid, marchid, mimpid), it will return NULL. Passing that NULL
pointer to strlen() would cause the tool to crash in environments without
/proc mounted or on older RISC-V kernels.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260513154851.1085334-1-guanli.oerv@isrc.iscas.ac.cn?part=2

  parent reply	other threads:[~2026-05-14  7:38 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-13 15:48 [PATCH 0/3] perf build: Fix cross-arch build failures and GCC 14 warnings Li Guan
2026-05-13 15:48 ` [PATCH 1/3] perf build: Fix cross-arch build failures by isolating auxtrace objects Li Guan
2026-05-13 16:14   ` Ian Rogers
2026-05-13 16:31     ` guanli
2026-05-13 16:37       ` Ian Rogers
2026-05-14  6:59   ` sashiko-bot
2026-05-13 15:48 ` [PATCH 2/3] perf riscv: Fix discarded const qualifier error in _get_field() Li Guan
2026-05-13 16:18   ` Ian Rogers
2026-05-13 18:07   ` [PATCH v2] perf riscv: Fix discarded const qualifier " Li Guan
2026-05-13 23:11     ` Ian Rogers
2026-05-14  7:38   ` sashiko-bot [this message]
2026-05-13 15:48 ` [PATCH 3/3] perf script: Provide weak stubs for instruction decoding Li Guan
2026-05-13 16:20   ` Ian Rogers
2026-05-14  8:06   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260514073825.95CE8C2BCC6@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=guanli.oerv@isrc.iscas.ac.cn \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox