diff mbox series

[24/38] trace-cmd: close file descriptor in trace_vsock_make()

Message ID 20240605134054.2626953-25-jmarchan@redhat.com (mailing list archive)
State Accepted
Commit 3c5347a6545830777887d1de3f91a0fd5e460e43
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
Close the socket file descriptor in the error paths.

Fixes a RESOURCE_LEAK error (CWE-772)

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
---
 tracecmd/trace-vsock.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/tracecmd/trace-vsock.c b/tracecmd/trace-vsock.c
index baa310f7..df1a9800 100644
--- a/tracecmd/trace-vsock.c
+++ b/tracecmd/trace-vsock.c
@@ -43,12 +43,16 @@  int __hidden trace_vsock_make(unsigned int port)
 	setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
 
 	if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)))
-		return -errno;
+		goto error;
 
 	if (listen(sd, SOMAXCONN))
-		return -errno;
+		goto error;
 
 	return sd;
+
+error:
+	close(sd);
+	return -errno;
 }
 
 int __hidden trace_vsock_make_any(void)