[CRIU] [PATCH 2/3] util: don't leak a socket

Andrey Vagin avagin at openvz.org
Thu Apr 7 10:29:43 PDT 2016


From: Andrew Vagin <avagin at virtuozzo.com>

CID 159478 (#2 of 2): Resource leak (RESOURCE_LEAK)
8. leaked_handle: Handle variable sk going out of scope leaks the handle.

Signed-off-by: Andrew Vagin <avagin at virtuozzo.com>
---
 criu/util.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/criu/util.c b/criu/util.c
index 024dd9e..4f88309 100644
--- a/criu/util.c
+++ b/criu/util.c
@@ -1189,17 +1189,18 @@ int setup_tcp_client(char *addr)
 
 	pr_info("Connecting to server %s:%u\n", addr, (int)ntohs(opts.port));
 
+	if (get_sockaddr_in(&saddr, addr))
+		return -1;
+
 	sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
 	if (sk < 0) {
 		pr_perror("Can't create socket");
 		return -1;
 	}
 
-	if (get_sockaddr_in(&saddr, addr))
-		return -1;
-
 	if (connect(sk, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
 		pr_perror("Can't connect to server");
+		close(sk);
 		return -1;
 	}
 
-- 
2.5.0



More information about the CRIU mailing list