Re: [PATCH v2 2/4] perf tools: Fix double free and memory leak in kvm_events_record()

[email protected] Mon, 03 Aug 2026 13:37:36 +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:

Pre-existing issues:
- [Medium] The patch addresses an array-mutation bug in `kvm_events_record(=
)` but leaves identical instances of the bug unfixed in `__cmd_record()`, `=
__cmd_report()`, `__cmd_buildid_list()`, and `__cmd_top()`.
--

--- Patch [2]: [PATCH v2 2/4] perf tools: Fix double free and memory leak i=
n kvm_events_record() ---
commit de06a9553df4c03dc3591b78a5d37cfb9f36b1e0
Author: Michalis Niarchos <[email protected]>

perf tools: Fix double free and memory leak in kvm_events_record()

cmd_record() reorders the contents of the rec_argv pointer array, causing
an issue where pointers are freed twice and others never freed. This patch
fixes the double free and memory leak by referencing literals directly
instead of duplicating them, removing the need to track and free each entry.

> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
> index 44c6998f2ee5d..45c92ab74fdd6 100644
> --- a/tools/perf/builtin-kvm.c
> +++ b/tools/perf/builtin-kvm.c

[ ... ]

> @@ -1717,9 +1717,6 @@ kvm_events_record(struct perf_kvm_stat *kvm, int ar=
gc, const char **argv)
>  	record_usage =3D kvm_stat_record_usage;
>  	ret =3D cmd_record(i, rec_argv);
> =20
> -EXIT:
> -	for (i =3D 0; i < rec_argc; i++)
> -		free((void *)rec_argv[i]);

[Severity: Medium]
This is a pre-existing issue, but does this exact same array-mutation bug s=
till
exist in other functions within this file?

It looks like __cmd_record(), __cmd_report(), __cmd_buildid_list(), and
__cmd_top() all have this identical cleanup loop:

	for (i =3D 0; i < rec_argc; i++)
		free((void *)rec_argv[i]);

Since these functions also pass the allocated rec_argv array to command han=
dlers
that use parse_options() (which consumes options in-place and shifts non-op=
tion
arguments forward), do these other commands also suffer from a double-free
and memory leak?

>  	free(rec_argv);
>  	return ret;
>  }

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