Skip to content

Commit

Permalink
fix(config): fix segfault at exit
Browse files Browse the repository at this point in the history
In case "auto_save" is not set in the config file, the error pointer
will point to a GError that is printed by the g_info() but the pointer
is not freed after using. This causes not only memory leak but also a
warning from Glib in case you also do not have "custom_color" in your
config as g_key_file_get_string() will get a pointer that already points
to GError. The warning message looks like this:

(swappy:492252): GLib-WARNING **: 20:11:27.212: GError set over the top of a previous GError or uninitialized memory.
This indicates a bug in someone's code. You must ensure an error is NULL before it's set.
The overwriting error message was: Key file does not have key “custom_color” in group “Default”

A solution is to properly free the error pointer after it is no longer
needed and before it is reused.

---------

Co-authored-by: Krzysztof Adamski <[email protected]>
  • Loading branch information
kadamski and Krzysztof Adamski authored Nov 4, 2024
1 parent ee663a6 commit 182322d
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ static void load_config_from_file(struct swappy_config *config,
config->auto_save = auto_save;
} else {
g_info("auto_save is missing in %s (%s)", file, error->message);
g_error_free(error);
error = NULL;
}

custom_color = g_key_file_get_string(gkf, group, "custom_color", &error);
Expand Down Expand Up @@ -272,7 +274,7 @@ static void load_default_config(struct swappy_config *config) {
config->early_exit = CONFIG_EARLY_EXIT_DEFAULT;
config->fill_shape = CONFIG_FILL_SHAPE_DEFAULT;
config->auto_save = CONFIG_AUTO_SAVE_DEFAULT;
config->custom_color = CONFIG_CUSTOM_COLOR_DEFAULT;
config->custom_color = g_strdup(CONFIG_CUSTOM_COLOR_DEFAULT);
}

void config_load(struct swappy_state *state) {
Expand Down

0 comments on commit 182322d

Please sign in to comment.