Commit 2716b487 by S.Çağlar Onur Committed by Serge Hallyn

fix memory leaks reported by cppcheck in src/lxc/lxc_usernsexec.c

Free previously allocated memory if realloc fails. Signed-off-by: 's avatarS.Çağlar Onur <caglar@10ur.org> Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 49aba4d3
...@@ -201,6 +201,7 @@ static int read_default_map(char *fnam, char which, char *username) ...@@ -201,6 +201,7 @@ static int read_default_map(char *fnam, char which, char *username)
if (line) if (line)
free(line); free(line);
fclose(fin); fclose(fin);
free(newmap);
return 0; return 0;
} }
...@@ -241,6 +242,7 @@ static int run_cmd(char **argv) ...@@ -241,6 +242,7 @@ static int run_cmd(char **argv)
static int map_child_uids(int pid, struct id_map *map) static int map_child_uids(int pid, struct id_map *map)
{ {
char **uidargs = NULL, **gidargs = NULL; char **uidargs = NULL, **gidargs = NULL;
char **newuidargs = NULL, **newgidargs = NULL;
int i, nuargs = 2, ngargs = 2; int i, nuargs = 2, ngargs = 2;
struct id_map *m; struct id_map *m;
...@@ -263,9 +265,12 @@ static int map_child_uids(int pid, struct id_map *map) ...@@ -263,9 +265,12 @@ static int map_child_uids(int pid, struct id_map *map)
for (m=map; m; m = m->next) { for (m=map; m; m = m->next) {
if (m->which == 'b' || m->which == 'u') { if (m->which == 'b' || m->which == 'u') {
nuargs += 3; nuargs += 3;
uidargs = realloc(uidargs, (nuargs+1) * sizeof(*uidargs)); newuidargs = realloc(uidargs, (nuargs+1) * sizeof(*uidargs));
if (!uidargs) if (!newuidargs) {
free(uidargs)
return -1; return -1;
}
uidargs = newuidargs;
uidargs[nuargs - 3] = malloc(21); uidargs[nuargs - 3] = malloc(21);
uidargs[nuargs - 2] = malloc(21); uidargs[nuargs - 2] = malloc(21);
uidargs[nuargs - 1] = malloc(21); uidargs[nuargs - 1] = malloc(21);
...@@ -278,9 +283,12 @@ static int map_child_uids(int pid, struct id_map *map) ...@@ -278,9 +283,12 @@ static int map_child_uids(int pid, struct id_map *map)
} }
if (m->which == 'b' || m->which == 'g') { if (m->which == 'b' || m->which == 'g') {
ngargs += 3; ngargs += 3;
gidargs = realloc(gidargs, (ngargs+1) * sizeof(*gidargs)); newgidargs = realloc(gidargs, (ngargs+1) * sizeof(*gidargs));
if (!gidargs) if (!newgidargs){
free(gidargs);
return -1; return -1;
}
gidargs = newgidargs;
gidargs[ngargs - 3] = malloc(21); gidargs[ngargs - 3] = malloc(21);
gidargs[ngargs - 2] = malloc(21); gidargs[ngargs - 2] = malloc(21);
gidargs[ngargs - 1] = malloc(21); gidargs[ngargs - 1] = malloc(21);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment