Blame libtracefs-Prevent-a-memory-leak-in-open_cpu_files.patch

Jerome Marchand 3772ca
From 8f2593fbbad2a549b854645acde7b11f5e02a924 Mon Sep 17 00:00:00 2001
Jerome Marchand 3772ca
From: Jerome Marchand <jmarchan@redhat.com>
Jerome Marchand 3772ca
Date: Thu, 6 Jun 2024 17:38:23 +0200
Jerome Marchand 3772ca
Subject: [PATCH 09/15] libtracefs: Prevent a memory leak in open_cpu_files()
Jerome Marchand 3772ca
Jerome Marchand 3772ca
In open_cpu_files(), if realloc() fails, the latest allocated tcpu
Jerome Marchand 3772ca
isn't freed. Rearrange the loop to prevent that.
Jerome Marchand 3772ca
Jerome Marchand 3772ca
Link: https://lore.kernel.org/linux-trace-devel/20240606153830.2666120-10-jmarchan@redhat.com
Jerome Marchand 3772ca
Jerome Marchand 3772ca
Fixes: 564bffddcb117 ("libtracefs: Use tracefs_cpu_read() for tracefs_iterate_raw_events()")
Jerome Marchand 3772ca
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
Jerome Marchand 3772ca
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Jerome Marchand 3772ca
---
Jerome Marchand 3772ca
 src/tracefs-events.c | 10 +++++-----
Jerome Marchand 3772ca
 1 file changed, 5 insertions(+), 5 deletions(-)
Jerome Marchand 3772ca
Jerome Marchand 3772ca
diff --git a/src/tracefs-events.c b/src/tracefs-events.c
Jerome Marchand 3772ca
index d65837e..88325e1 100644
Jerome Marchand 3772ca
--- a/src/tracefs-events.c
Jerome Marchand 3772ca
+++ b/src/tracefs-events.c
Jerome Marchand 3772ca
@@ -275,9 +275,12 @@ static int open_cpu_files(struct tracefs_instance *instance, cpu_set_t *cpus,
Jerome Marchand 3772ca
 			tcpu = tracefs_cpu_snapshot_open(instance, cpu, true);
Jerome Marchand 3772ca
 		else
Jerome Marchand 3772ca
 			tcpu = tracefs_cpu_open_mapped(instance, cpu, true);
Jerome Marchand 3772ca
+		if (!tcpu)
Jerome Marchand 3772ca
+			goto error;
Jerome Marchand 3772ca
+
Jerome Marchand 3772ca
 		tmp = realloc(*all_cpus, (i + 1) * sizeof(*tmp));
Jerome Marchand 3772ca
 		if (!tmp) {
Jerome Marchand 3772ca
-			i--;
Jerome Marchand 3772ca
+			tracefs_cpu_close(tcpu);
Jerome Marchand 3772ca
 			goto error;
Jerome Marchand 3772ca
 		}
Jerome Marchand 3772ca
 
Jerome Marchand 3772ca
@@ -285,9 +288,6 @@ static int open_cpu_files(struct tracefs_instance *instance, cpu_set_t *cpus,
Jerome Marchand 3772ca
 
Jerome Marchand 3772ca
 		memset(tmp + i, 0, sizeof(*tmp));
Jerome Marchand 3772ca
 
Jerome Marchand 3772ca
-		if (!tcpu)
Jerome Marchand 3772ca
-			goto error;
Jerome Marchand 3772ca
-
Jerome Marchand 3772ca
 		tmp[i].tcpu = tcpu;
Jerome Marchand 3772ca
 		tmp[i].cpu = cpu;
Jerome Marchand 3772ca
 		i++;
Jerome Marchand 3772ca
@@ -296,7 +296,7 @@ static int open_cpu_files(struct tracefs_instance *instance, cpu_set_t *cpus,
Jerome Marchand 3772ca
 	return 0;
Jerome Marchand 3772ca
  error:
Jerome Marchand 3772ca
 	tmp = *all_cpus;
Jerome Marchand 3772ca
-	for (; i >= 0; i--) {
Jerome Marchand 3772ca
+	for (i--; i >= 0; i--) {
Jerome Marchand 3772ca
 		tracefs_cpu_close(tmp[i].tcpu);
Jerome Marchand 3772ca
 	}
Jerome Marchand 3772ca
 	free(tmp);
Jerome Marchand 3772ca
-- 
Jerome Marchand 3772ca
2.45.2
Jerome Marchand 3772ca