Blob Blame History Raw
From 1c9539491d2440eab01ffc3b7a616502ef22cc1b Mon Sep 17 00:00:00 2001
From: Jerome Marchand <jmarchan@redhat.com>
Date: Thu, 6 Jun 2024 17:38:17 +0200
Subject: [PATCH 03/15] libtracefs: Prevent a memory leak in update_fields()

In update_fields() p isn't freed is store_str fails. Since p is to be
freed whatever the result of store_str() might be, move the call to
free() before checking field_name.

Fixes a RESSOURCE_LEAK error (CWE-772)

Link: https://lore.kernel.org/linux-trace-devel/20240606153830.2666120-4-jmarchan@redhat.com

Fixes: fa51df99ad21d ("libtracefs: Allow for simple SQL statements to create a histogram")
Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 src/tracefs-sqlhist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/tracefs-sqlhist.c b/src/tracefs-sqlhist.c
index ecf09ce..c7b9eff 100644
--- a/src/tracefs-sqlhist.c
+++ b/src/tracefs-sqlhist.c
@@ -810,9 +810,9 @@ static int update_fields(struct tep_handle *tep,
 			if (!p)
 				return -1;
 			field_name = store_str(sb, p);
+			free((char *)p);
 			if (!field_name)
 				return -1;
-			free((char *)p);
 		}
 
 		tfield = tep_find_any_field(event, field_name);
-- 
2.45.2