Re: [PATCH 1/4] perf tools: Fix memory leak in cmd_kvm()

[email protected] Mon, 03 Aug 2026 10:42:06 +0000
Newsgroups org.kernel.vger.linux-perf-users
Message-ID <[email protected]>
Thank you for your contribution! Sashiko AI review found 1 potential issue(=
s) to consider:
- [Low] The error check evaluating `if (!file_name)` immediately following =
`get_filename_for_perf_kvm()` is functionally dead code and prints an obsol=
ete error message.
--

--- Patch [1]: [PATCH 1/4] perf tools: Fix memory leak in cmd_kvm() ---
commit 85aa78ecdd5ada6c616d04d112b0f46542981162
Author: Michalis Niarchos <[email protected]>

perf tools: Fix memory leak in cmd_kvm()

filename may get allocated by get_filename_for_perf_kvm(), but is
never freed. Use string literals to remove the need for freeing.

> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
> index 394302ebdb161..44c6998f2ee5d 100644
> --- a/tools/perf/builtin-kvm.c
> +++ b/tools/perf/builtin-kvm.c
> @@ -606,11 +606,11 @@ static const char *get_filename_for_perf_kvm(void)
>  	const char *filename;
> =20
>  	if (perf_host && !perf_guest)
> -		filename =3D strdup("perf.data.host");
> +		filename =3D "perf.data.host";
>  	else if (!perf_host && perf_guest)
> -		filename =3D strdup("perf.data.guest");
> +		filename =3D "perf.data.guest";
>  	else
> -		filename =3D strdup("perf.data.kvm");
> +		filename =3D "perf.data.kvm";
> =20
>  	return filename;
>  }

[Severity: Low]
Since get_filename_for_perf_kvm() now exclusively returns static string
literals, it can never return NULL.

Is it worth cleaning up the now-dead error handling block in cmd_kvm() that
still checks for allocation failures?

tools/perf/builtin-kvm.c:cmd_kvm() {
    ...
	if (!file_name) {
		file_name =3D get_filename_for_perf_kvm();

		if (!file_name) {
			pr_err("Failed to allocate memory for filename\n");
			return -ENOMEM;
		}
	}
    ...
}

--=20
Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20260803-perf-kvm-f=
[email protected]?part=3D1