From 8cd813403960ae2038ec49bcc22c3a49af9c3664 Mon Sep 17 00:00:00 2001
From: tanyifeng <tanyifeng1@huawei.com>
Date: Mon, 14 Jan 2019 21:24:25 +0800
Subject: [PATCH 027/131] fix bug of memory leak

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/conf.c         | 11 +++++++++++
 src/lxc/conf.h         |  1 +
 src/lxc/lxccontainer.c | 11 +++++++----
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 6a14de1f..e076bf28 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -4822,6 +4822,16 @@ int lxc_clear_procs(struct lxc_conf *c, const char *key)
 	return 0;
 }
 
+int lxc_clear_namespace(struct lxc_conf *c)
+{
+	int i;
+	for (i = 0; i < LXC_NS_MAX; i++) {
+		free(c->ns_share[i]);
+		c->ns_share[i] = NULL;
+	}
+	return 0;
+}
+
 int lxc_clear_groups(struct lxc_conf *c)
 {
 	struct lxc_list *it, *next;
@@ -5036,6 +5046,7 @@ void lxc_conf_free(struct lxc_conf *conf)
 	lxc_clear_limits(conf, "lxc.prlimit");
 	lxc_clear_sysctls(conf, "lxc.sysctl");
 	lxc_clear_procs(conf, "lxc.proc");
+	lxc_clear_namespace(conf);
 	free(conf->cgroup_meta.dir);
 	free(conf->cgroup_meta.controllers);
 	/* isulad add begin */
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 2263e47e..44feb986 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -491,6 +491,7 @@ extern int setup_sysctl_parameters(struct lxc_list *sysctls);
 extern int lxc_clear_sysctls(struct lxc_conf *c, const char *key);
 extern int setup_proc_filesystem(struct lxc_list *procs, pid_t pid);
 extern int lxc_clear_procs(struct lxc_conf *c, const char *key);
+extern int lxc_clear_namespace(struct lxc_conf *c);
 
 /* isulad add begin */
 int lxc_clear_init_args(struct lxc_conf *lxc_conf);
diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index 81c0ec35..e6272fcd 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -5418,10 +5418,13 @@ int list_active_containers(const char *lxcpath, char ***nret,
 				continue;
 		}
 
-		if (array_contains(&ct_name, p, ct_name_cnt)) {
-			if (is_hashed)
-				free(p);
-			continue;
+
+		if (ct_name && 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)) {
-- 
2.23.0

