diff mbox series

[17/38] trace-cmd lib: prevent memory leak in ptp_clock_server()

Message ID 20240605134054.2626953-18-jmarchan@redhat.com (mailing list archive)
State Accepted
Commit 1103a591d63c88c1ea1fb9871d7399a37b5674ad
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
The pointer results is allocated in tracecmd_msg_recv_time_sync(). It
isn't freed if ptp_clock_server() exits with an error. Free it in the
error path.

Fixes a RESOURCE_LEAK error (CWE-772)

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
 lib/trace-cmd/trace-timesync-ptp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-timesync-ptp.c b/lib/trace-cmd/trace-timesync-ptp.c
index 20e6e6f1..402edeb2 100644
--- a/lib/trace-cmd/trace-timesync-ptp.c
+++ b/lib/trace-cmd/trace-timesync-ptp.c
@@ -608,8 +608,10 @@  static int ptp_clock_server(struct tracecmd_time_sync *tsync,
 					  sync_proto, &sync_msg,
 					  &size, (char **)&results);
 	if (ret || strncmp(sync_proto, PTP_NAME, TRACECMD_TSYNC_PNAME_LENGTH) ||
-	    sync_msg != PTP_SYNC_PKT_PROBES || size == 0 || results == NULL)
+	    sync_msg != PTP_SYNC_PKT_PROBES || size == 0 || results == NULL) {
+		free(results);
 		return -1;
+	}
 
 	ntoh_ptp_results(results);
 	if (ptp->flags & PTP_FLAG_USE_MARKER)