diff mbox series

[06/15] libtracefs: don't leak socket file descriptor in open_vsock()

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

Commit Message

Jerome Marchand June 6, 2024, 3:38 p.m. UTC
Close the socket file descriptor if connect() fails.

Fixes a RESSOURCE_LEAK error (CWE-772)

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

Patch

diff --git a/src/tracefs-vsock.c b/src/tracefs-vsock.c
index e171382..9171321 100644
--- a/src/tracefs-vsock.c
+++ b/src/tracefs-vsock.c
@@ -19,8 +19,10 @@  static int open_vsock(unsigned int cid, unsigned int port)
 	if (sd < 0)
 		return -1;
 
-	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)))
+	if (connect(sd, (struct sockaddr *)&addr, sizeof(addr))) {
+		close(sd);
 		return -1;
+	}
 
 	return sd;
 }