[PATCH, next, v2] perf iostat: Release the port list if command allocation fails

From: lirongqing

Date: Mon Nov 09 2026 - 04:05:46 EST


From: Li RongQing <lirongqing@xxxxxxxxx>

iostat_event_group() takes ownership of the root port list and frees it
via the err: label on both the normal and parsing-error paths. When
calloc() for iostat_cmd fails, however, the function returns -ENOMEM
directly, leaking the list, its pointer array and every port in it.

Route this failure through the existing cleanup path instead. At this
point no port has been transferred to an event yet, so
iio_root_ports_list_free() releases the whole list, and the trailing
free(iostat_cmd) is a no-op on the NULL pointer.

Fixes: f9ed693e8bc0 ("perf stat: Enable iostat mode for x86 platforms")
Signed-off-by: Li RongQing <lirongqing@xxxxxxxxx>
---
Diff with v1: rebase to next

tools/perf/arch/x86/util/iostat.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/arch/x86/util/iostat.c b/tools/perf/arch/x86/util/iostat.c
index 7442a2c..d2cc2e7 100644
--- a/tools/perf/arch/x86/util/iostat.c
+++ b/tools/perf/arch/x86/util/iostat.c
@@ -310,8 +310,10 @@ static int iostat_event_group(struct evlist *evl,
int metrics_count = iostat_metrics_count();
char *iostat_cmd = calloc(len_template, 1);

- if (!iostat_cmd)
- return -ENOMEM;
+ if (!iostat_cmd) {
+ ret = -ENOMEM;
+ goto err;
+ }

for (idx = 0; idx < list->nr_entries; idx++) {
sprintf(iostat_cmd, iostat_cmd_template,
--
2.9.4