Skip to content

Commit

Permalink
Issue #24: Fixing bad realloc() (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
speters authored Jan 20, 2018
1 parent 0b6d57b commit 9dbd14a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,24 @@ ssize_t recvSync(int fd, char *wait, char **recv)
}

rptr = *recv;
size_t i = 0;
while ((count = readn(fd, &c, 1))) {
alarm(0);
if (count < 0) {
continue;
}

*rptr++ = c;
*(rptr + 1) = '\0';
i++;
if (! ((rptr - *recv + 1) % ALLOCSIZE)) {
if (realloc(*recv, ALLOCSIZE * sizeof(char) * ++rcount) == NULL) {
char *tmp = realloc(*recv, ALLOCSIZE * sizeof(char) * ++rcount);
if (tmp == NULL) {
logIT1(LOG_ERR, "realloc error");
exit(1);
} else {
*recv = tmp;
rptr = *recv + i;
}
}

Expand All @@ -107,9 +114,12 @@ ssize_t recvSync(int fd, char *wait, char **recv)

}

if (! realloc(*recv, strlen(*recv) + 1)) {
char *tmp = realloc(*recv, strlen(*recv) + 1);
if (tmp == NULL) {
logIT1(LOG_ERR, "realloc error");
exit(1);
} else {
*recv = tmp;
}

if (count <= 0) {
Expand Down

0 comments on commit 9dbd14a

Please sign in to comment.