mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-07-24 20:52:16 +00:00
coverity: change flow to safely free in all exit paths
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
parent
88dd66fc36
commit
ce42ba2c79
@ -258,13 +258,17 @@ 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));
|
||||
gidargs = malloc(3 * sizeof(*gidargs));
|
||||
if (uidargs == NULL || gidargs == NULL)
|
||||
if (uidargs == NULL)
|
||||
return -1;
|
||||
gidargs = malloc(3 * sizeof(*gidargs));
|
||||
if (gidargs == NULL) {
|
||||
free(uidargs);
|
||||
return -1;
|
||||
}
|
||||
uidargs[0] = malloc(10);
|
||||
gidargs[0] = malloc(10);
|
||||
uidargs[1] = malloc(21);
|
||||
@ -272,7 +276,7 @@ static int map_child_uids(int pid, struct id_map *map)
|
||||
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);
|
||||
@ -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);
|
||||
@ -299,16 +301,14 @@ 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);
|
||||
@ -316,17 +316,20 @@ static int map_child_uids(int pid, struct id_map *map)
|
||||
}
|
||||
}
|
||||
|
||||
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++)
|
||||
@ -334,7 +337,7 @@ static int map_child_uids(int pid, struct id_map *map)
|
||||
free(uidargs);
|
||||
free(gidargs);
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
Loading…
Reference in New Issue
Block a user