Skip to content

Commit

Permalink
Merge pull request #88 from MaxXor/memory-leaks
Browse files Browse the repository at this point in the history
Fix memory leaks on failure
  • Loading branch information
f0rb1dd3n committed Jun 27, 2020
2 parents 10137d4 + 0a95012 commit 1e17bc8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 3 additions & 2 deletions userland/client/client.c
Expand Up @@ -492,8 +492,9 @@ char *read_line(void)

if (position >= bufsize) {
bufsize += RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
char *buffer_backup = buffer;
if ((buffer = realloc(buffer, bufsize)) == NULL) {
free(buffer_backup);
fprintf(stderr, "reptile: allocation error\n");
exit(EXIT_FAILURE);
}
Expand Down
17 changes: 9 additions & 8 deletions userland/client/listener.c
Expand Up @@ -223,15 +223,15 @@ int shell(int sock, char **args)
while (args[len] != NULL) {
size++;
size += strlen(args[len]);
temp = (char *)realloc(temp, size);
char *temp_backup = temp;
if ((temp = realloc(temp, size)) == NULL) {
free(temp_backup);
p_error("realloc");
return 1;
}
len++;
}

if (temp == NULL) {
p_error("realloc");
return 1;
}

memset(temp, '\0', size);

for (i = 0; i < len; i++) {
Expand Down Expand Up @@ -614,8 +614,9 @@ char *read_line(void)

if (position >= bufsize) {
bufsize += RL_BUFSIZE;
buffer = realloc(buffer, bufsize);
if (!buffer) {
char *buffer_backup = buffer;
if ((buffer = realloc(buffer, bufsize)) == NULL) {
free(buffer_backup);
fprintf(stderr, "reptile: allocation error\n");
exit(EXIT_FAILURE);
}
Expand Down
4 changes: 3 additions & 1 deletion userland/shell.c
Expand Up @@ -163,8 +163,10 @@ int runshell(int client)

pid = fork();

if (pid < 0)
if (pid < 0) {
free(temp);
return (ERROR);
}

if (pid == 0) {
close(client);
Expand Down

0 comments on commit 1e17bc8

Please sign in to comment.