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

Commit 83bc75a

Browse files
authored
Merge pull request #304 from bergwolf/coverity
fix a couple of Coverity warnings
2 parents 823a7a2 + e4bbebd commit 83bc75a

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

src/exec.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ static int hyper_setup_exec_user(struct hyper_exec *exec)
197197

198198
uid_t uid = 0;
199199
gid_t gid = 0;
200-
int ngroups;
200+
int ngroups = 0;
201201
gid_t *reallocgroups, *groups = NULL;
202202

203203
// check the config
@@ -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: 12 additions & 5 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

@@ -546,12 +550,15 @@ static void hyper_print_uptime(void)
546550
{
547551
char buf[128];
548552
int fd = open("/proc/uptime", O_RDONLY);
553+
int n;
549554

550555
if (fd < 0)
551556
return;
552-
memset(buf, 0, sizeof(buf));
553-
if (read(fd, buf, sizeof(buf)))
557+
n = read(fd, buf, sizeof(buf)-1);
558+
if (n > 0) {
559+
buf[n] = 0;
554560
fprintf(stdout, "uptime %s\n", buf);
561+
}
555562

556563
close(fd);
557564
}

src/net.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ int hyper_setup_hostname(struct hyper_pod *pod)
683683
int hyper_write_dns_file(int fd, char *field, char **data, int num)
684684
{
685685
int i = 0, len = 0, ret = -1, size;
686-
char *buf = NULL;
686+
char *newbuf, *buf = NULL;
687687

688688
if (num == 0)
689689
return 0;
@@ -699,11 +699,12 @@ int hyper_write_dns_file(int fd, char *field, char **data, int num)
699699
for (i = 0; i < num; i++) {
700700
// 1 for space
701701
int new_size = size + strlen(data[i]) + 1;
702-
buf = realloc(buf, new_size);
703-
if (buf == NULL) {
702+
newbuf = realloc(buf, new_size + 1);
703+
if (newbuf == NULL) {
704704
fprintf(stderr, "fail to realloc buff for %s\n", field);
705705
goto out;
706706
}
707+
buf = newbuf;
707708
buf[size]= ' ';
708709
memcpy(buf + size + 1, data[i], strlen(data[i]));
709710
fprintf(stdout, "%s: data: %s\n", field, buf);

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
@@ -683,8 +683,12 @@ int hyper_open_channel(char *channel, int mode)
683683
fd = open(path, O_RDONLY);
684684

685685
memset(name, 0, sizeof(name));
686-
if (fd < 0 || read(fd, name, sizeof(name)) < 0)
686+
if (fd < 0)
687+
continue;
688+
if (read(fd, name, sizeof(name)) < 0) {
689+
close(fd);
687690
continue;
691+
}
688692

689693
close(fd);
690694
fd = -1;

0 commit comments

Comments
 (0)