Skip to content

Commit

Permalink
coverity: change flow to safely free in all exit paths
Browse files Browse the repository at this point in the history
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
  • Loading branch information
hallyn committed Dec 12, 2013
1 parent 88dd66f commit ce42ba2
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/lxc/lxc_usernsexec.c
Expand Up @@ -258,21 +258,25 @@ 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;
int i, nuargs = 2, ngargs = 2, ret = -1;
struct id_map *m;

uidargs = malloc(3 * sizeof(*uidargs));
if (uidargs == NULL)
return -1;
gidargs = malloc(3 * sizeof(*gidargs));
if (uidargs == NULL || gidargs == NULL)
if (gidargs == NULL) {
free(uidargs);
return -1;
}
uidargs[0] = malloc(10);
gidargs[0] = malloc(10);
uidargs[1] = malloc(21);
gidargs[1] = malloc(21);
uidargs[2] = NULL;
gidargs[2] = NULL;
if (!uidargs[0] || !uidargs[1] || !gidargs[0] || !gidargs[1])
return -1;
goto out;
sprintf(uidargs[0], "newuidmap");
sprintf(gidargs[0], "newgidmap");
sprintf(uidargs[1], "%d", pid);
Expand All @@ -281,16 +285,14 @@ static int map_child_uids(int pid, struct id_map *map)
if (m->which == 'b' || m->which == 'u') {
nuargs += 3;
newuidargs = realloc(uidargs, (nuargs+1) * sizeof(*uidargs));
if (!newuidargs) {
free(uidargs);
return -1;
}
if (!newuidargs)
goto out;
uidargs = newuidargs;
uidargs[nuargs - 3] = malloc(21);
uidargs[nuargs - 2] = malloc(21);
uidargs[nuargs - 1] = malloc(21);
if (!uidargs[nuargs-3] || !uidargs[nuargs-2] || !uidargs[nuargs-1])
return -1;
goto out;
sprintf(uidargs[nuargs - 3], "%ld", m->ns_id);
sprintf(uidargs[nuargs - 2], "%ld", m->host_id);
sprintf(uidargs[nuargs - 1], "%ld", m->range);
Expand All @@ -299,42 +301,43 @@ static int map_child_uids(int pid, struct id_map *map)
if (m->which == 'b' || m->which == 'g') {
ngargs += 3;
newgidargs = realloc(gidargs, (ngargs+1) * sizeof(*gidargs));
if (!newgidargs){
free(gidargs);
return -1;
}
if (!newgidargs)
goto out;
gidargs = newgidargs;
gidargs[ngargs - 3] = malloc(21);
gidargs[ngargs - 2] = malloc(21);
gidargs[ngargs - 1] = malloc(21);
if (!gidargs[ngargs-3] || !gidargs[ngargs-2] || !gidargs[ngargs-1])
return -1;
goto out;
sprintf(gidargs[ngargs - 3], "%ld", m->ns_id);
sprintf(gidargs[ngargs - 2], "%ld", m->host_id);
sprintf(gidargs[ngargs - 1], "%ld", m->range);
gidargs[ngargs] = NULL;
}
}

ret = -2;
// exec newuidmap
if (nuargs > 2 && run_cmd(uidargs) != 0) {
fprintf(stderr, "Error mapping uids\n");
return -2;
goto out;
}
// exec newgidmap
if (ngargs > 2 && run_cmd(gidargs) != 0) {
fprintf(stderr, "Error mapping gids\n");
return -2;
goto out;
}
ret = 0;

out:
for (i=0; i<nuargs; i++)
free(uidargs[i]);
for (i=0; i<ngargs; i++)
free(gidargs[i]);
free(uidargs);
free(gidargs);

return 0;
return ret;
}

int main(int argc, char *argv[])
Expand Down

0 comments on commit ce42ba2

Please sign in to comment.