Skip to content

Commit 3120d0b

Browse files
Fix memory leak during configuration parsing (openstreetmap#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 <[email protected]>
1 parent 0478a1b commit 3120d0b

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/renderd.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ void request_exit(void)
173173
// Any write to the exit pipe will trigger a graceful exit
174174
char c = 0;
175175

176+
g_logger(G_LOG_LEVEL_INFO, "Sending exit request");
177+
176178
if (write(exit_pipe_fd, &c, sizeof(c)) < 0) {
177179
g_logger(G_LOG_LEVEL_ERROR, "Failed to write to the exit pipe: %s", strerror(errno));
178180
}
@@ -212,10 +214,10 @@ void process_loop(int listen_fd)
212214
num = poll(pfd, num_cslots + PFD_SPECIAL_COUNT, -1);
213215

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

@@ -893,6 +895,8 @@ int main(int argc, char **argv)
893895
process_loop(fd);
894896

895897
unlink(config.socketname);
898+
free_map_sections(maps);
899+
free_renderd_sections(config_slaves);
896900
close(fd);
897901
return 0;
898902
}

src/renderd_config.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void process_map_sections(const char *config_file_name, xmlconfigitem *maps_dest
214214
const char *section = iniparser_getsecname(ini, section_num);
215215

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

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

278-
while ((ini_type_part = strtok_r(ini_type_copy, " ", &ini_type_copy))) {
278+
for (ini_type_part = strtok_r(ini_type_copy, " ", &ini_type_context);
279+
ini_type_part;
280+
ini_type_part = strtok_r(NULL, " ", &ini_type_context)) {
279281
switch (ini_type_part_num) {
280282
case 0:
281283
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
315317
*/
316318
maps_dest[map_section_num].num_threads = num_threads;
317319

320+
free(ini_type_copy);
318321
free(ini_type_part);
319322
free((void *)ini_type);
320323
}

0 commit comments

Comments
 (0)