[lxc-devel] [PATCH] free previously allocated memory if realloc fails in src/lxc/lsm/apparmor.c

S.Çağlar Onur caglar at 10ur.org
Fri Nov 15 05:02:28 UTC 2013


Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
---
 src/lxc/lsm/apparmor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/lxc/lsm/apparmor.c b/src/lxc/lsm/apparmor.c
index aaf8056..f7f2ff9 100644
--- a/src/lxc/lsm/apparmor.c
+++ b/src/lxc/lsm/apparmor.c
@@ -68,7 +68,7 @@ static char *apparmor_process_label_get(pid_t pid)
 {
 	char path[100], *space;
 	int ret;
-	char *buf = NULL;
+	char *buf = NULL, *newbuf;
 	int sz = 0;
 	FILE *f;
 
@@ -88,14 +88,16 @@ again:
 		return NULL;
 	}
 	sz += 1024;
-	buf = realloc(buf, sz);
-	if (!buf) {
+	newbuf = realloc(buf, sz);
+	if (!newbuf) {
+		free(buf);
 		ERROR("out of memory");
 		process_lock();
 		fclose(f);
 		process_unlock();
 		return NULL;
 	}
+	buf = newbuf;
 	memset(buf, 0, sz);
 	ret = fread(buf, 1, sz - 1, f);
 	process_lock();
-- 
1.8.3.2





More information about the lxc-devel mailing list