From 3120d0b7618f702b608cc94343059927aa2942ad Mon Sep 17 00:00:00 2001 From: Hummeltech <6109326+hummeltech@users.noreply.github.com> Date: Sat, 29 Jun 2024 13:47:17 -0700 Subject: [PATCH] Fix memory leak during configuration parsing (#455) 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 --- src/renderd.c | 8 ++++++-- src/renderd_config.c | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/renderd.c b/src/renderd.c index 9119cbc9..6a805359 100644 --- a/src/renderd.c +++ b/src/renderd.c @@ -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)); } @@ -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; } @@ -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; } diff --git a/src/renderd_config.c b/src/renderd_config.c index c1669361..a745cb74 100644 --- a/src/renderd_config.c +++ b/src/renderd_config.c @@ -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; @@ -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); @@ -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); }