diff mbox series

[05/38] trace-cmd lib: prevent possible memory coruption in add_plugin_file()

Message ID 20240605134054.2626953-6-jmarchan@redhat.com (mailing list archive)
State Accepted
Commit 2143a05a21ff38aba00395460dbed8465c59e14a
Headers show
Series trace-cmd: fix misc issues found by static analysis | expand

Commit Message

Jerome Marchand June 5, 2024, 1:40 p.m. UTC
If strdup() fails the error path access original address of
pdata->files after it has been dereferenced. Make sure that
pdata->files contains the up-to-date pointer before calling calling a
function that could fail.

This was flagged as ressource leak (CWE-772) because ptr isn't freed
in that scenario, but there is something worse going on that the
static analysis missed.

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

Patch

diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
index b5002f1d..47ca3db1 100644
--- a/lib/trace-cmd/trace-util.c
+++ b/lib/trace-cmd/trace-util.c
@@ -279,11 +279,11 @@  static void add_plugin_file(struct tep_handle *pevent, const char *path,
 	if (!ptr)
 		goto out_free;
 
+	pdata->files = ptr;
 	ptr[pdata->index] = strdup(name);
 	if (!ptr[pdata->index])
 		goto out_free;
 
-	pdata->files = ptr;
 	pdata->index++;
 	pdata->files[pdata->index] = NULL;
 	return;