[lxc-devel] [lxc/master] Fix memory leak in list_active_containers

lifeng68 on Github lxc-bot at linuxcontainers.org
Tue Apr 24 10:38:46 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 344 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180424/a4fe9d32/attachment.bin>
-------------- next part --------------
From 1c9b9258288874ac3dfa61aa6a06aa503ce6c02e Mon Sep 17 00:00:00 2001
From: LiFeng <lifeng68 at huawei.com>
Date: Tue, 24 Apr 2018 15:10:15 -0400
Subject: [PATCH] Fix memory leak in list_active_containers

Signed-off-by: LiFeng <lifeng68 at huawei.com>
---
 src/lxc/lxccontainer.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 6d41b6cf1..8d0cbc61c 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -4973,29 +4973,43 @@ int list_active_containers(const char *lxcpath, char ***nret,
 			char *recvpath = lxc_cmd_get_lxcpath(p);
 			if (!recvpath)
 				continue;
-			if (strncmp(lxcpath, recvpath, lxcpath_len) != 0)
+			if (strncmp(lxcpath, recvpath, lxcpath_len) != 0) {
+				free(recvpath);
 				continue;
+			}
+			free(recvpath);
 			p = lxc_cmd_get_name(p);
 			if (!p)
 				continue;
 		}
 
-		if (array_contains(&ct_name, p, ct_name_cnt))
+		if (array_contains(&ct_name, p, ct_name_cnt)) {
+			if (is_hashed)
+				free(p);
 			continue;
+		}
 
-		if (!add_to_array(&ct_name, p, ct_name_cnt))
+		if (!add_to_array(&ct_name, p, ct_name_cnt)) {
+			if (is_hashed)
+				free(p);
 			goto free_cret_list;
+		}
 
 		ct_name_cnt++;
 
-		if (!cret)
+		if (!cret) {
+			if (is_hashed)
+				free(p);
 			continue;
+		}
 
 		c = lxc_container_new(p, lxcpath);
 		if (!c) {
 			INFO("Container %s:%s is running but could not be loaded",
 				lxcpath, p);
 			remove_from_array(&ct_name, p, ct_name_cnt--);
+			if (is_hashed)
+				free(p);
 			continue;
 		}
 
@@ -5007,14 +5021,20 @@ int list_active_containers(const char *lxcpath, char ***nret,
 
 		if (!add_to_clist(cret, c, cret_cnt, true)) {
 			lxc_container_put(c);
+			if (is_hashed)
+				free(p);
 			goto free_cret_list;
 		}
 		cret_cnt++;
+
+		if (is_hashed)
+			free(p);
 	}
 
 	if (nret && cret && cret_cnt != ct_name_cnt) {
 		if (c)
 			lxc_container_put(c);
+
 		goto free_cret_list;
 	}
 


More information about the lxc-devel mailing list