Skip to content

Commit

Permalink
Fix memory leak during configuration parsing (#455)
Browse files Browse the repository at this point in the history
The memory allocated by `strndup` needs to be freed.

The code has also been changed to not lose the allocation pointer and according to the semantics of `strtok_r` explained here:
https://man.freebsd.org/cgi/man.cgi?query=strtok_r

Also improved messaging.

Co-authored-by: Roland Bosa <[email protected]>
  • Loading branch information
hummeltech and rolandbosa authored Jun 29, 2024
1 parent 0478a1b commit 3120d0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/renderd.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void request_exit(void)
// Any write to the exit pipe will trigger a graceful exit
char c = 0;

g_logger(G_LOG_LEVEL_INFO, "Sending exit request");

if (write(exit_pipe_fd, &c, sizeof(c)) < 0) {
g_logger(G_LOG_LEVEL_ERROR, "Failed to write to the exit pipe: %s", strerror(errno));
}
Expand Down Expand Up @@ -212,10 +214,10 @@ void process_loop(int listen_fd)
num = poll(pfd, num_cslots + PFD_SPECIAL_COUNT, -1);

if (num == -1) {
g_logger(G_LOG_LEVEL_ERROR, "poll(): %s", strerror(errno));
g_logger(G_LOG_LEVEL_DEBUG, "poll(): %s", strerror(errno));
} else if (num) {
if (pfd[PFD_EXIT_PIPE].revents & POLLIN) {
// A render thread wants us to exit
g_logger(G_LOG_LEVEL_INFO, "Received exit request, exiting process_loop");
break;
}

Expand Down Expand Up @@ -893,6 +895,8 @@ int main(int argc, char **argv)
process_loop(fd);

unlink(config.socketname);
free_map_sections(maps);
free_renderd_sections(config_slaves);
close(fd);
return 0;
}
Expand Down
7 changes: 5 additions & 2 deletions src/renderd_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void process_map_sections(const char *config_file_name, xmlconfigitem *maps_dest
const char *section = iniparser_getsecname(ini, section_num);

if (strncmp(section, "renderd", 7) && strcmp(section, "mapnik")) { // this is a map config section
char *ini_type_copy, *ini_type_part;
char *ini_type_copy, *ini_type_part, *ini_type_context;
const char *ini_type;
int ini_type_part_maxlen = 64, ini_type_part_num = 0;

Expand Down Expand Up @@ -275,7 +275,9 @@ void process_map_sections(const char *config_file_name, xmlconfigitem *maps_dest
process_config_string(ini, section, "type", &ini_type, "png image/png png256", INILINE_MAX);
ini_type_copy = strndup(ini_type, INILINE_MAX);

while ((ini_type_part = strtok_r(ini_type_copy, " ", &ini_type_copy))) {
for (ini_type_part = strtok_r(ini_type_copy, " ", &ini_type_context);
ini_type_part;
ini_type_part = strtok_r(NULL, " ", &ini_type_context)) {
switch (ini_type_part_num) {
case 0:
copy_string(ini_type_part, &maps_dest[map_section_num].file_extension, ini_type_part_maxlen);
Expand Down Expand Up @@ -315,6 +317,7 @@ void process_map_sections(const char *config_file_name, xmlconfigitem *maps_dest
*/
maps_dest[map_section_num].num_threads = num_threads;

free(ini_type_copy);
free(ini_type_part);
free((void *)ini_type);
}
Expand Down

0 comments on commit 3120d0b

Please sign in to comment.