[CRIU] [PATCH] parasite: fix error paths in parasite_init

Andrey Vagin avagin at openvz.org
Sat Apr 6 05:28:39 EDT 2013


* Call restore_ns on error paths.
* Use close_safe for the socket, because it can be reused

CID 996194 (#1 of 3): Resource leak (RESOURCE_LEAK)
6. leaked_handle: Handle variable "rst" going out of scope leaks the
handle.

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 parasite-syscall.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/parasite-syscall.c b/parasite-syscall.c
index d82995e..653db25 100644
--- a/parasite-syscall.c
+++ b/parasite-syscall.c
@@ -309,18 +309,19 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
 		}
 
 		sock = socket(PF_UNIX, SOCK_DGRAM, 0);
-		if (sock < 0) {
+		if (sock < 0)
 			pr_perror("Can't create socket");
+
+		if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
+			return -1;
+		if (sock < 0)
 			return -1;
-		}
 
 		if (bind(sock, (struct sockaddr *)&args->h_addr, args->h_addr_len) < 0) {
 			pr_perror("Can't bind socket");
 			goto err;
 		}
 
-		if (rst > 0 && restore_ns(rst, &net_ns_desc) < 0)
-			goto err;
 	} else {
 		struct sockaddr addr = { .sa_family = AF_UNSPEC, };
 
@@ -350,7 +351,7 @@ static int parasite_init(struct parasite_ctl *ctl, pid_t pid, int nr_threads)
 	ctl->tsock = sock;
 	return 0;
 err:
-	close(sock);
+	close_safe(&sock);
 	return -1;
 }
 
-- 
1.7.11.7



More information about the CRIU mailing list