Free previously allocated memory if realloc fails.

Signed-off-by: S.Çağlar Onur <cag...@10ur.org>
---
 src/lxc/lxc_usernsexec.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxc_usernsexec.c b/src/lxc/lxc_usernsexec.c
index 9416f27..2957dd5 100644
--- a/src/lxc/lxc_usernsexec.c
+++ b/src/lxc/lxc_usernsexec.c
@@ -201,6 +201,7 @@ static int read_default_map(char *fnam, char which, char 
*username)
        if (line)
                free(line);
        fclose(fin);
+       free(newmap);
        return 0;
 }
 
@@ -241,6 +242,7 @@ static int run_cmd(char **argv)
 static int map_child_uids(int pid, struct id_map *map)
 {
        char **uidargs = NULL, **gidargs = NULL;
+       char **newuidargs = NULL, **newgidargs = NULL;
        int i, nuargs = 2, ngargs = 2;
        struct id_map *m;
 
@@ -263,9 +265,12 @@ static int map_child_uids(int pid, struct id_map *map)
        for (m=map; m; m = m->next) {
                if (m->which == 'b' || m->which == 'u') {
                        nuargs += 3;
-                       uidargs = realloc(uidargs, (nuargs+1) * 
sizeof(*uidargs));
-                       if (!uidargs)
+                       newuidargs = realloc(uidargs, (nuargs+1) * 
sizeof(*uidargs));
+                       if (!newuidargs) {
+                               free(uidargs)
                                return -1;
+                       }
+                       uidargs = newuidargs;
                        uidargs[nuargs - 3] = malloc(21);
                        uidargs[nuargs - 2] = malloc(21);
                        uidargs[nuargs - 1] = malloc(21);
@@ -278,9 +283,12 @@ static int map_child_uids(int pid, struct id_map *map)
                }
                if (m->which == 'b' || m->which == 'g') {
                        ngargs += 3;
-                       gidargs = realloc(gidargs, (ngargs+1) * 
sizeof(*gidargs));
-                       if (!gidargs)
+                       newgidargs = realloc(gidargs, (ngargs+1) * 
sizeof(*gidargs));
+                       if (!newgidargs){
+                               free(gidargs);
                                return -1;
+                       }
+                       gidargs = newgidargs;
                        gidargs[ngargs - 3] = malloc(21);
                        gidargs[ngargs - 2] = malloc(21);
                        gidargs[ngargs - 1] = malloc(21);
-- 
1.8.3.2


------------------------------------------------------------------------------
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing 
conversations that shape the rapidly evolving mobile landscape. Sign up now. 
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
_______________________________________________
Lxc-devel mailing list
Lxc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lxc-devel

Reply via email to