[lxc-devel] [PATCH 3/4] coverity: fix leak in error case

Dwight Engen dwight.engen at oracle.com
Fri May 3 16:04:01 UTC 2013


Since lxc_execute() is available through the library and is exposed via
the API we cannot be sure the caller will immediately exit, so we should
take care to free the allocated memory.

Signed-off-by: Dwight Engen <dwight.engen at oracle.com>
---
 src/lxc/execute.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/lxc/execute.c b/src/lxc/execute.c
index d93e8e1..9bf33ca 100644
--- a/src/lxc/execute.c
+++ b/src/lxc/execute.c
@@ -55,7 +55,7 @@ static char *choose_init(void)
 	ret = snprintf(retv, PATH_MAX, LXCINITDIR "/lxc/lxc-init");
 	if (ret < 0 || ret >= PATH_MAX) {
 		ERROR("pathname too long");
-		return NULL;
+		goto out1;
 	}
 
 	ret = stat(retv, &mystat);
@@ -65,7 +65,7 @@ static char *choose_init(void)
 	ret = snprintf(retv, PATH_MAX, "/usr/lib/lxc/lxc-init");
 	if (ret < 0 || ret >= PATH_MAX) {
 		ERROR("pathname too long");
-		return NULL;
+		goto out1;
 	}
 	ret = stat(retv, &mystat);
 	if (ret == 0)
@@ -73,11 +73,13 @@ static char *choose_init(void)
 	ret = snprintf(retv, PATH_MAX, "/sbin/lxc-init");
 	if (ret < 0 || ret >= PATH_MAX) {
 		ERROR("pathname too long");
-		return NULL;
+		goto out1;
 	}
 	ret = stat(retv, &mystat);
 	if (ret == 0)
 		return retv;
+out1:
+	free(retv);
 	return NULL;
 }
 
-- 
1.8.1.4





More information about the lxc-devel mailing list