Re: [PATCH 2/4] perf tools: Fix memory leak in kvm_events_report()

[email protected] Mon, 03 Aug 2026 10:43:47 +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:
- [Medium] Invalid free of a string literal causes a segmentation fault.
--

--- Patch [2]: [PATCH 2/4] perf tools: Fix memory leak in kvm_events_report=
() ---
commit b736af0d386356e57597151f619466500011da0a
Author: Michalis Niarchos <[email protected]>

perf tools: Fix memory leak in kvm_events_report()

This commit attempts to fix a memory leak in kvm_events_report() by freeing
the usage string after parse_options() completes.

> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
> --- a/tools/perf/builtin-kvm.c
> +++ b/tools/perf/builtin-kvm.c
> @@ -1755,6 +1755,9 @@ kvm_events_report(struct perf_kvm_stat *kvm, int ar=
gc, const char **argv)
>  		if (argc)
>  			usage_with_options(kvm_events_report_usage,
>  					   kvm_events_report_options);
> +
> +		/* free usage string allocated by parse_options */
> +		free((void *)kvm_events_report_usage[0]);

[Severity: Medium]
Will this result in an invalid free and a segmentation fault?

Looking at the definition of kvm_events_report_usage in builtin-kvm.c:

const char * const kvm_events_report_usage[] =3D {
        "perf kvm stat report [<options>]",
        NULL
};

Since parse_options() bypasses allocation when subcommands is NULL, it
appears kvm_events_report_usage[0] remains pointing to the statically
allocated string literal rather than dynamically allocated memory. Freeing a
read-only string literal will crash the perf tool unconditionally on every
successful invocation.

>  	}
> =20
>  #ifndef HAVE_SLANG_SUPPORT

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