Skip to content

Commit

Permalink
Fixed mem. leaks and invalid writes in set_fd_filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoCostaIFG committed Apr 3, 2020
1 parent acd3dbd commit 0a3c334
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/utls.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ int set_fd_filter(void) {

/* alloc memory and assemble filter */
if (filter == NULL) { // first item
filter = (char *)malloc(sizeof(char) * (strlen(fd_info) + 2));
if ((filter = (char *)malloc(sizeof(char) * (strlen(fd_info) + 2))) == NULL)
exit_log(MALLOC_FAIL);
strcpy(filter, fd_info);
filter[fd_info_len] = '\n';
filter[fd_info_len + 1] = '\0';
} else {
filter_len = strlen(filter) + strlen(fd_info);
filter = realloc(filter, sizeof(char) * (filter_len + 1));
filter = realloc(filter, sizeof(char) * (filter_len + 2));

strcat(filter, fd_info);
filter[filter_len] = '\n';
Expand All @@ -118,10 +119,14 @@ int set_fd_filter(void) {
}
}

closedir(dirp);

if (filter == NULL) // nothing to filter
filter = "";
return setenv(FILTER_ENV, "", 1);

return setenv(FILTER_ENV, filter, 1);
int ret = setenv(FILTER_ENV, filter, 1);
free(filter);
return ret;
}

int get_filtered_fd(int *upstream_fd) {
Expand Down Expand Up @@ -180,6 +185,8 @@ int get_filtered_fd(int *upstream_fd) {
}
}

closedir(dirp);

if (candidate_num != 1)
return 1;

Expand Down

0 comments on commit 0a3c334

Please sign in to comment.