diff mbox series

[05/15] libtracefs: prevent memory leak in tracefs_event_systems()

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

Commit Message

Jerome Marchand June 6, 2024, 3:38 p.m. UTC
If an error is encountered in the loop, enable and sys might not get
freed. Move the calls to free() before possibly exiting the loop.

Fixes a RESSOURCE_LEAK error (CWE-772)

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

Patch

diff --git a/src/tracefs-events.c b/src/tracefs-events.c
index 1b1693c..906dbe8 100644
--- a/src/tracefs-events.c
+++ b/src/tracefs-events.c
@@ -854,12 +854,12 @@  char **tracefs_event_systems(const char *tracing_dir)
 		enable = trace_append_file(sys, "enable");
 
 		ret = stat(enable, &st);
+		free(enable);
+		free(sys);
 		if (ret >= 0) {
 			if (add_list_string(&systems, name) < 0)
 				goto out_free;
 		}
-		free(enable);
-		free(sys);
 	}
 
 	closedir(dir);