[lxc-devel] [PATCH 2/2] lxc_start: free the conf if starting the container fails

Weng Meiling wengmeiling.weng at huawei.com
Fri May 3 03:02:48 UTC 2013


When running lxc-start command with valgrind, it reports a memory leak error.
When lxc-start command fails, the conf which is from malloc has not been released.
This patch fix the problem.

Signed-off-by: Weng Meiling <wengmeiling.weng at huawei.com>
---
 src/lxc/lxc_start.c |   21 +++++++++++----------
 1 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
index 957fdb0..014c37b 100644
--- a/src/lxc/lxc_start.c
+++ b/src/lxc/lxc_start.c
@@ -196,25 +196,25 @@ int main(int argc, char *argv[])

 	if (rcfile && lxc_config_read(rcfile, conf)) {
 		ERROR("failed to read configuration file");
-		return err;
+		goto out;
 	}

 	if (lxc_config_define_load(&defines, conf))
-		return err;
+		goto out;

 	if (!rcfile && !strcmp("/sbin/init", args[0])) {
 		ERROR("no configuration file for '/sbin/init' (may crash the host)");
-		return err;
+		goto out;
 	}

 	if (ensure_path(&conf->console.path, my_args.console) < 0) {
 		ERROR("failed to ensure console path '%s'", my_args.console);
-		return err;
+		goto out;
 	}

 	if (ensure_path(&conf->console.log_path, my_args.console_log) < 0) {
 		ERROR("failed to ensure console log '%s'", my_args.console_log);
-		return err;
+		goto out;
 	}

 	if (my_args.pidfile != NULL) {
@@ -222,7 +222,7 @@ int main(int argc, char *argv[])
 		if (pid_fp == NULL) {
 			SYSERROR("failed to create pidfile '%s' for '%s'",
 				 my_args.pidfile, my_args.name);
-			return err;
+			goto out;
 		}
 	}

@@ -232,19 +232,19 @@ int main(int argc, char *argv[])

 		if (!lxc_caps_check()) {
 			ERROR("Not running with sufficient privilege");
-			return err;
+			goto out;
 		}

 		if (daemon(0, 0)) {
 			SYSERROR("failed to daemonize '%s'", my_args.name);
-			return err;
+			goto out;
 		}
 	}

 	if (pid_fp != NULL) {
 		if (fprintf(pid_fp, "%d\n", getpid()) < 0) {
 			SYSERROR("failed to write '%s'", my_args.pidfile);
-			return err;
+			goto out;
 		}
 		fclose(pid_fp);
 	}
@@ -267,7 +267,8 @@ int main(int argc, char *argv[])

 	if (my_args.pidfile)
 		unlink(my_args.pidfile);
-
+out:
+	lxc_conf_free(conf);
 	return err;
 }

-- 1.7.1





More information about the lxc-devel mailing list