From 0f794e8150eb79d55f8409f48bc5ea0c6ff91d02 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Thu, 13 Jun 2024 07:48:40 +0100 Subject: [PATCH] options: fix segfault on start struct options::all_scripts is not initialized if no animation/fading is defined in config file. Later if fading is enable on the command line, we will try to push into a NULL dynarr. Make sure all_scripts is always initialized. Signed-off-by: Yuxuan Shui --- src/config.c | 2 ++ src/config_libconfig.c | 14 +++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/config.c b/src/config.c index c662755f48..5d4eb64a75 100644 --- a/src/config.c +++ b/src/config.c @@ -22,6 +22,7 @@ #include "common.h" #include "log.h" +#include "utils/dynarr.h" #include "utils/kernel.h" #include "utils/str.h" @@ -719,5 +720,6 @@ bool parse_config(options_t *opt, const char *config_file) { // clang-format on list_init_head(&opt->included_config_files); + opt->all_scripts = dynarr_new(struct script *, 4); return parse_config_libconfig(opt, config_file); } diff --git a/src/config_libconfig.c b/src/config_libconfig.c index 969c9ede45..f074851ba8 100644 --- a/src/config_libconfig.c +++ b/src/config_libconfig.c @@ -387,18 +387,17 @@ parse_animation_one(struct win_script *animations, config_setting_t *setting) { return script; } -static struct script ** -parse_animations(struct win_script *animations, config_setting_t *setting) { +/// `out_scripts`: all the script objects created, this is a dynarr. +static void parse_animations(struct win_script *animations, config_setting_t *setting, + struct script ***out_scripts) { auto number_of_animations = (unsigned)config_setting_length(setting); - auto all_scripts = dynarr_new(struct script *, number_of_animations + 1); for (unsigned i = 0; i < number_of_animations; i++) { auto sub = config_setting_get_elem(setting, (unsigned)i); auto script = parse_animation_one(animations, sub); if (script != NULL) { - dynarr_push(all_scripts, script); + dynarr_push(*out_scripts, script); } } - return all_scripts; } #define FADING_TEMPLATE_1 \ @@ -975,10 +974,7 @@ bool parse_config_libconfig(options_t *opt, const char *config_file) { config_setting_t *animations = config_lookup(&cfg, "animations"); if (animations) { - opt->all_scripts = parse_animations(opt->animations, animations); - } else { - // Reserve some space for generated fading scripts. - opt->all_scripts = dynarr_new(struct script *, 4); + parse_animations(opt->animations, animations, &opt->all_scripts); } opt->config_file_path = path;