Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit cf829f1

Browse files
committed
fix resource leaks reported by coverity
Signed-off-by: Peng Tao <[email protected]>
1 parent ec39c70 commit cf829f1

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/exec.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static int hyper_setup_exec_user(struct hyper_exec *exec)
261261
struct group *gr = hyper_getgrnam(group);
262262
if (gr == NULL) {
263263
perror("can't find the group");
264-
return -1;
264+
goto fail;
265265
}
266266
gid = gr->gr_gid;
267267
}
@@ -414,8 +414,12 @@ static int hyper_install_process_stdio(struct hyper_exec *e, struct stdio_config
414414

415415
sprintf(ptmx, "/dev/pts/%d", e->ptyno);
416416
ptyslave = open(ptmx, O_RDWR | O_CLOEXEC);
417-
if (ptyslave < 0 || ioctl(ptyslave, TIOCSCTTY, NULL) < 0) {
417+
if (ptyslave < 0) {
418+
perror("open pty device for execcmd failed");
419+
goto out;
420+
} if (ioctl(ptyslave, TIOCSCTTY, NULL) < 0) {
418421
perror("ioctl pty device for execcmd failed");
422+
close(ptyslave);
419423
goto out;
420424
}
421425
io->stdinfd = ptyslave;

src/init.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void hyper_term_all(struct hyper_pod *pod)
132132
int pid;
133133
DIR *dp;
134134
struct dirent *de;
135-
pid_t *pids = NULL;
135+
pid_t *pidsnew, *pids = NULL;
136136
struct hyper_exec *e;
137137
pid_t hyperstart_pid;
138138

@@ -152,9 +152,13 @@ static void hyper_term_all(struct hyper_pod *pod)
152152
if (pid == hyperstart_pid)
153153
continue;
154154
if (index <= npids) {
155-
pids = realloc(pids, npids + 16384);
156-
if (pids == NULL)
155+
pidsnew = realloc(pids, npids + 16384);
156+
if (pidsnew == NULL) {
157+
free(pids);
158+
closedir(dp);
157159
return;
160+
}
161+
pids = pidsnew;
158162
npids += 16384;
159163
}
160164

src/parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ static int hyper_parse_string_array(char *json, jsmntok_t *toks, char *field,
10751075
dbg_pr(stdout, "%s count %d\n", field, *num);
10761076

10771077
values = calloc(*num, sizeof(*values));
1078-
if (data == NULL) {
1078+
if (values == NULL) {
10791079
dbg_pr(stdout, "alloc memory for %s failed\n", field);
10801080
return -1;
10811081
}

src/util.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,12 @@ int hyper_open_channel(char *channel, int mode)
676676
fd = open(path, O_RDONLY);
677677

678678
memset(name, 0, sizeof(name));
679-
if (fd < 0 || read(fd, name, sizeof(name)) < 0)
679+
if (fd < 0)
680+
continue;
681+
if (read(fd, name, sizeof(name)) < 0) {
682+
close(fd);
680683
continue;
684+
}
681685

682686
close(fd);
683687
fd = -1;

0 commit comments

Comments
 (0)