Skip to content

Commit

Permalink
config: fix memory leaks in config string handling and cleanup
Browse files Browse the repository at this point in the history
String handling in config parsing to prevent memory leaks by
duplicating strings and properly freeing resources.

Signed-off-by: Charalampos Mitrodimas <[email protected]>
  • Loading branch information
charmitro committed Feb 19, 2024
1 parent 57c97f6 commit b73c5f7
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ void config_split_array_string(char *dest_array[], const char *value, int *len)
char *str_ = strtok(temp_value, ",");
while (str_ != NULL && idx < 10) {
remove_spaces(str_);
dest_array[idx++] = str_;
dest_array[idx] = strdup(str_);
if (dest_array[idx] == NULL) {
free(temp_value);
return;
}
idx++;
str_ = strtok(NULL, ",");
}

*len = idx;
free(temp_value); // Free the duplicated string after tokenizing.
}

config_t *ini_config;
Expand Down Expand Up @@ -196,6 +202,13 @@ void free_config(void)

free((void *)ini_config->smtp_config->from);

for (int i = 0; i < ini_config->smtp_config->to_len; i++) {
free(ini_config->smtp_config->to[i]);
}
for (int i = 0; i < ini_config->smtp_config->cc_len; i++) {
free(ini_config->smtp_config->cc[i]);
}

free(ini_config->smtp_config);
free(ini_config->postgres_config);
free(ini_config);
Expand Down

0 comments on commit b73c5f7

Please sign in to comment.