diff mbox series

[03/15] libtracefs: prevent a memory leak in update_fields()

Message ID 20240606153830.2666120-4-jmarchan@redhat.com (mailing list archive)
State Accepted
Commit 1c9539491d2440eab01ffc3b7a616502ef22cc1b
Headers show
Series libtracefs: fix misc issues found by static analysis | expand

Commit Message

Jerome Marchand June 6, 2024, 3:38 p.m. UTC
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)

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
 src/tracefs-sqlhist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox series

Patch

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);