From b22322bfd104949b93d1df22ff36689747917448 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 15 Nov 2021 19:22:06 +0100 Subject: config: letter-spacing: add missing ‘return’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The missing return caused us to fall through to the bottom of the function, where we ended with an error message: [main].letter-spacing: 0: not a valid option: letter-spacing Closes #795 --- config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'config.c') diff --git a/config.c b/config.c index 905f26d6..2f12e201 100644 --- a/config.c +++ b/config.c @@ -956,7 +956,7 @@ parse_section_main(struct context *ctx) return value_to_pt_or_px(ctx, &conf->line_height); else if (strcmp(key, "letter-spacing") == 0) - value_to_pt_or_px(ctx, &conf->letter_spacing); + return value_to_pt_or_px(ctx, &conf->letter_spacing); else if (strcmp(key, "horizontal-letter-offset") == 0) return value_to_pt_or_px(ctx, &conf->horizontal_letter_offset); -- cgit v1.3 From 02bb2768f176164099a30b74504c86e1accb7bb1 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 19 Nov 2021 16:51:52 +0100 Subject: config: Remove some "UNREACHABLE" UB footguns Every branch of these long if/else if/else chains returns, so the compiler can figure out on its own that the last line is never reached. If, for some reason, one of the branches does not return (as was the case after 205f1f7, fixed by b22322b), this would usually result in a compiler error ("control reaches end of non-void function"), but adding UNREACHABLE transforms this into silent undefined behaviour. --- config.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 2f12e201..c655813d 100644 --- a/config.c +++ b/config.c @@ -1012,8 +1012,6 @@ parse_section_main(struct context *ctx) LOG_CONTEXTUAL_ERR("not a valid option: %s", key); return false; } - - UNREACHABLE(); } static bool @@ -2340,8 +2338,6 @@ parse_section_tweak(struct context *ctx) LOG_CONTEXTUAL_ERR("not a valid option: %s", key); return false; } - - UNREACHABLE(); } static bool -- cgit v1.3 From 877c1e6f07df5110dd08d6c6aa01987ca1b46a24 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 20 Nov 2021 11:58:01 +0100 Subject: config: Simplify render-timer option handling --- config.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index c655813d..f8273484 100644 --- a/config.c +++ b/config.c @@ -2263,29 +2263,10 @@ parse_section_tweak(struct context *ctx) return false; } - switch (mode) { - case 0: - conf->tweak.render_timer_osd = false; - conf->tweak.render_timer_log = false; - return true; - - case 1: - conf->tweak.render_timer_osd = true; - conf->tweak.render_timer_log = false; - return true; - - case 2: - conf->tweak.render_timer_osd = false; - conf->tweak.render_timer_log = true; - return true; - - case 3: - conf->tweak.render_timer_osd = true; - conf->tweak.render_timer_log = true; - return true; - } - - UNREACHABLE(); + xassert(0 <= mode && mode <= 3); + conf->tweak.render_timer_osd = mode == 1 || mode == 3; + conf->tweak.render_timer_log = mode == 2 || mode == 3; + return true; } else if (strcmp(key, "delayed-render-lower") == 0) { -- cgit v1.3 From 0db125a1253d2daa33d94c7f1a50b22eeb176be3 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 20 Nov 2021 15:12:56 +0000 Subject: config: fix memory leak in value_to_enum() This could be triggered by running, for example: foot -o tweak.render-timer=invalid For some reason LeakSanitizer was only detecting this leak when the option value was invalid, even though it was occuring in either case. --- config.c | 1 + 1 file changed, 1 insertion(+) (limited to 'config.c') diff --git a/config.c b/config.c index 2f12e201..50306b9d 100644 --- a/config.c +++ b/config.c @@ -614,6 +614,7 @@ value_to_enum(struct context *ctx, const char **value_map, int *res) valid_values[idx - 2] = '\0'; LOG_CONTEXTUAL_ERR("not one of %s", valid_values); + free(valid_values); *res = -1; return false; } -- cgit v1.3 From e89e735995382de9b5dd8df5d1f45ed9f766f06a Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Sat, 20 Nov 2021 15:23:24 +0000 Subject: config: use stack allocation for temporary buffer in value_to_enum() The maximum size required for the string buffer is relatively small, known at compile-time and not likely to ever grow beyond 512 bytes. Therefore we may as well just use the stack. --- config.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 50306b9d..24ed1267 100644 --- a/config.c +++ b/config.c @@ -27,6 +27,7 @@ #include "util.h" #include "wayland.h" #include "xmalloc.h" +#include "xsnprintf.h" static const uint32_t default_foreground = 0xdcdccc; static const uint32_t default_background = 0x111111; @@ -604,17 +605,17 @@ value_to_enum(struct context *ctx, const char **value_map, int *res) } const size_t size = str_len + count * 4 + 1; - char *valid_values = xmalloc(size); - int idx = 0; + char valid_values[512]; + size_t idx = 0; + xassert(size < sizeof(valid_values)); for (size_t i = 0; i < count; i++) - idx += snprintf(&valid_values[idx], size - idx, "'%s', ", value_map[i]); + idx += xsnprintf(&valid_values[idx], size - idx, "'%s', ", value_map[i]); if (count > 0) valid_values[idx - 2] = '\0'; LOG_CONTEXTUAL_ERR("not one of %s", valid_values); - free(valid_values); *res = -1; return false; } -- cgit v1.3 From 31c1b91a7c215135ec1c4a64e39853db9377fd91 Mon Sep 17 00:00:00 2001 From: Arnavion Date: Sat, 20 Nov 2021 20:49:01 -0800 Subject: Fix parsing key-bindings config to not read past the end of the string. pipe_argv_from_value itself stripped the cmd-to-exec from ctx->value, so the callers should not be doing that themselves. Fixes #809 --- config.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 24ed1267..9e290c65 100644 --- a/config.c +++ b/config.c @@ -1639,12 +1639,12 @@ argv_compare(char *const *argv1, char *const *argv2) * filled with {'cmd-to-exec', 'arg1', 'arg2', NULL} * * Returns: - * - ssize_t, number of bytes to strip from 'value' to remove the '[]' + * - ssize_t, number of bytes that were stripped from 'value' to remove the '[]' * enclosed cmd and its arguments, including any subsequent * whitespace characters. I.e. if 'value' is "[cmd] BTN_RIGHT", the * return value is 6 (strlen("[cmd] ")). * - cmd: allocated string containing "cmd arg1 arg2...". Caller frees. - * - argv: allocatd array containing {"cmd", "arg1", "arg2", NULL}. Caller frees. + * - argv: allocated array containing {"cmd", "arg1", "arg2", NULL}. Caller frees. */ static ssize_t pipe_argv_from_value(struct context *ctx, char ***argv) @@ -1729,8 +1729,6 @@ parse_key_binding_section(struct context *ctx, if (pipe_remove_len < 0) return false; - ctx->value += pipe_remove_len; - for (int action = 0; action < action_count; action++) { if (action_map[action] == NULL) continue; @@ -2107,8 +2105,6 @@ parse_section_mouse_bindings(struct context *ctx) if (pipe_remove_len < 0) return false; - value += pipe_remove_len; - for (enum bind_action_normal action = 0; action < BIND_ACTION_COUNT; action++) -- cgit v1.3 From 9b232e07f9b24a16d496939ec44c37fb772cd0b7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 21 Nov 2021 10:55:57 +0100 Subject: config: create a copy of the section name The parsing context keeps a pointer to the current section name. This is used when logging errors. However, the pointer was into a buffer allocated by getline(). This buffer are often re-used in the next call to getline(), or free:d. Regardless, the section name pointer is invalid after the next call to getline(), which meant all error messages were logging a correct section name. --- CHANGELOG.md | 1 + config.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'config.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index b65f0b63..1f7208d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ * Regression: `letter-spacing` resulting in a “not a valid option” error (https://codeberg.org/dnkl/foot/issues/795). +* Regression: bad section name in configuration error messages. ### Security diff --git a/config.c b/config.c index 24ed1267..fdca96cd 100644 --- a/config.c +++ b/config.c @@ -2469,9 +2469,11 @@ parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_ar continue; \ } + char *section_name = xstrdup("main"); + struct context context = { .conf = conf, - .section = "main", + .section = section_name, .path = path, .lineno = 0, .errors_are_fatal = errors_are_fatal, @@ -2539,7 +2541,9 @@ parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_ar error_or_continue(); } - context.section = &key_value[1]; + free(section_name); + section_name = xstrdup(&key_value[1]); + context.section = section_name; /* Process next line */ continue; @@ -2569,10 +2573,12 @@ parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_ar error_or_continue(); } + free(section_name); free(_line); return true; err: + free(section_name); free(_line); return false; } -- cgit v1.3 From 9174e6d79a1a57016973ef989ff9366b713b3f01 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 21 Nov 2021 17:51:46 +0100 Subject: config: pipe_argv_from_value(): plug memory leak When parsing a key binding with a pipe-argv, we failed to free the argv vector (or to be precise, the strdup:ed entries in the array) when failing to parse the remainder of the binding. --- config.c | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index eac9d3f5..60029ab9 100644 --- a/config.c +++ b/config.c @@ -1645,9 +1645,9 @@ argv_compare(char *const *argv1, char *const *argv2) * - argv: allocated array containing {"cmd", "arg1", "arg2", NULL}. Caller frees. */ static ssize_t -pipe_argv_from_value(struct context *ctx, char ***argv) +pipe_argv_from_value(struct context *ctx, struct argv *argv) { - *argv = NULL; + argv->args = NULL; if (ctx->value[0] != '[') return 0; @@ -1661,7 +1661,7 @@ pipe_argv_from_value(struct context *ctx, char ***argv) size_t pipe_len = pipe_cmd_end - ctx->value - 1; char *cmd = xstrndup(&ctx->value[1], pipe_len); - if (!tokenize_cmdline(cmd, argv)) { + if (!tokenize_cmdline(cmd, &argv->args)) { LOG_CONTEXTUAL_ERR("syntax error in command line"); free(cmd); return -1; @@ -1721,7 +1721,7 @@ parse_key_binding_section(struct context *ctx, const char *const action_map[static action_count], struct config_key_binding_list *bindings) { - char **pipe_argv; + struct argv pipe_argv; ssize_t pipe_remove_len = pipe_argv_from_value(ctx, &pipe_argv); if (pipe_remove_len < 0) @@ -1736,8 +1736,8 @@ parse_key_binding_section(struct context *ctx, /* Unset binding */ if (strcasecmp(ctx->value, "none") == 0) { - remove_action_from_key_bindings_list(bindings, action, pipe_argv); - free(pipe_argv); + remove_action_from_key_bindings_list(bindings, action, pipe_argv.args); + free_argv(&pipe_argv); return true; } @@ -1746,12 +1746,12 @@ parse_key_binding_section(struct context *ctx, has_key_binding_collisions( ctx, action, action_map, bindings, &key_combos)) { - free(pipe_argv); + free_argv(&pipe_argv); free_key_combo_list(&key_combos); return false; } - remove_action_from_key_bindings_list(bindings, action, pipe_argv); + remove_action_from_key_bindings_list(bindings, action, pipe_argv.args); /* Emit key bindings */ size_t ofs = bindings->count; @@ -1767,9 +1767,7 @@ parse_key_binding_section(struct context *ctx, .modifiers = combo->modifiers, .sym = combo->sym, .pipe = { - .argv = { - .args = pipe_argv, - }, + .argv = pipe_argv, .master_copy = first, }, }; @@ -1784,7 +1782,7 @@ parse_key_binding_section(struct context *ctx, } LOG_CONTEXTUAL_ERR("not a valid action: %s", ctx->key); - free(pipe_argv); + free_argv(&pipe_argv); return false; } @@ -2097,7 +2095,7 @@ parse_section_mouse_bindings(struct context *ctx) const char *key = ctx->key; const char *value = ctx->value; - char **pipe_argv; + struct argv pipe_argv; ssize_t pipe_remove_len = pipe_argv_from_value(ctx, &pipe_argv); if (pipe_remove_len < 0) @@ -2125,7 +2123,7 @@ parse_section_mouse_bindings(struct context *ctx) binding->action = BIND_ACTION_NONE; } } - free(pipe_argv); + free_argv(&pipe_argv); return true; } @@ -2133,7 +2131,7 @@ parse_section_mouse_bindings(struct context *ctx) if (!value_to_mouse_combos(ctx, &key_combos) || has_mouse_binding_collisions(ctx, &key_combos)) { - free(pipe_argv); + free_argv(&pipe_argv); free_key_combo_list(&key_combos); return false; } @@ -2143,9 +2141,9 @@ parse_section_mouse_bindings(struct context *ctx) struct config_mouse_binding *binding = &conf->bindings.mouse.arr[i]; if (binding->action == action && - ((binding->pipe.argv.args == NULL && pipe_argv == NULL) || - (binding->pipe.argv.args != NULL && pipe_argv != NULL && - argv_compare(binding->pipe.argv.args, pipe_argv) == 0))) + ((binding->pipe.argv.args == NULL && pipe_argv.args == NULL) || + (binding->pipe.argv.args != NULL && pipe_argv.args != NULL && + argv_compare(binding->pipe.argv.args, pipe_argv.args) == 0))) { if (binding->pipe.master_copy) free_argv(&binding->pipe.argv); @@ -2169,9 +2167,7 @@ parse_section_mouse_bindings(struct context *ctx) .button = combo->m.button, .count = combo->m.count, .pipe = { - .argv = { - .args = pipe_argv, - }, + .argv = pipe_argv, .master_copy = first, }, }; @@ -2185,7 +2181,7 @@ parse_section_mouse_bindings(struct context *ctx) } LOG_CONTEXTUAL_ERR("not a valid option: %s", key); - free(pipe_argv); + free_argv(&pipe_argv); return false; } -- cgit v1.3 From cdb2d90b893f35326146dbc657952265f1bf61ac Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 21 Nov 2021 18:04:36 +0100 Subject: config: argv_compare(): change parameters to ‘struct argv *’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of passing raw char** pointers to argv_compare(), pass pointers to ‘struct argv’. This lets argv_compare() handle both argv’s being NULL, or one of them being NULL. That is, the caller no longer needs to check that both argv’s are non-NULL before calling argv_compare(). Furthermore, update has_key_binding_collisions() to require the pipe argv to match, in addition to matching the ‘action’, when allowing a key collision. --- config.c | 86 ++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 46 insertions(+), 40 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 60029ab9..68061cc3 100644 --- a/config.c +++ b/config.c @@ -1557,11 +1557,40 @@ err: return false; } +static int +argv_compare(const struct argv *argv1, const struct argv *argv2) +{ + if (argv1->args == NULL && argv2->args == NULL) + return 0; + + if (argv1->args == NULL) + return -1; + if (argv2->args == NULL) + return 1; + + for (size_t i = 0; ; i++) { + if (argv1->args[i] == NULL && argv2->args[i] == NULL) + return 0; + if (argv1->args[i] == NULL) + return -1; + if (argv2->args[i] == NULL) + return 1; + + int ret = strcmp(argv1->args[i], argv2->args[i]); + if (ret != 0) + return ret; + } + + BUG("unexpected loop break"); + return 1; +} + static bool has_key_binding_collisions(struct context *ctx, int action, const char *const action_map[], const struct config_key_binding_list *bindings, - const struct key_combo_list *key_combos) + const struct key_combo_list *key_combos, + const struct argv *pipe_argv) { for (size_t j = 0; j < bindings->count; j++) { const struct config_key_binding *combo1 = &bindings->arr[j]; @@ -1569,8 +1598,10 @@ has_key_binding_collisions(struct context *ctx, if (combo1->action == BIND_ACTION_NONE) continue; - if (combo1->action == action) - continue; + if (combo1->action == action) { + if (argv_compare(&combo1->pipe.argv, pipe_argv) == 0) + continue; + } for (size_t i = 0; i < key_combos->count; i++) { const struct key_combo *combo2 = &key_combos->combos[i]; @@ -1600,29 +1631,6 @@ has_key_binding_collisions(struct context *ctx, return false; } -static int -argv_compare(char *const *argv1, char *const *argv2) -{ - xassert(argv1 != NULL); - xassert(argv2 != NULL); - - for (size_t i = 0; ; i++) { - if (argv1[i] == NULL && argv2[i] == NULL) - return 0; - if (argv1[i] == NULL) - return -1; - if (argv2[i] == NULL) - return 1; - - int ret = strcmp(argv1[i], argv2[i]); - if (ret != 0) - return ret; - } - - BUG("unexpected loop break"); - return 1; -} - /* * Parses a key binding value on the form * "[cmd-to-exec arg1 arg2] Mods+Key" @@ -1680,7 +1688,7 @@ pipe_argv_from_value(struct context *ctx, struct argv *argv) static void NOINLINE remove_action_from_key_bindings_list(struct config_key_binding_list *bindings, - int action, char **pipe_argv) + int action, const struct argv *pipe_argv) { size_t remove_first_idx = 0; size_t remove_count = 0; @@ -1688,11 +1696,10 @@ remove_action_from_key_bindings_list(struct config_key_binding_list *bindings, for (size_t i = 0; i < bindings->count; i++) { struct config_key_binding *binding = &bindings->arr[i]; - if (binding->action == action && - ((binding->pipe.argv.args == NULL && pipe_argv == NULL) || - (binding->pipe.argv.args != NULL && pipe_argv != NULL && - argv_compare(binding->pipe.argv.args, pipe_argv) == 0))) - { + if (binding->action != action) + continue; + + if (argv_compare(&binding->pipe.argv, pipe_argv) == 0) { if (remove_count++ == 0) remove_first_idx = i; @@ -1736,7 +1743,7 @@ parse_key_binding_section(struct context *ctx, /* Unset binding */ if (strcasecmp(ctx->value, "none") == 0) { - remove_action_from_key_bindings_list(bindings, action, pipe_argv.args); + remove_action_from_key_bindings_list(bindings, action, &pipe_argv); free_argv(&pipe_argv); return true; } @@ -1744,14 +1751,14 @@ parse_key_binding_section(struct context *ctx, struct key_combo_list key_combos = {0}; if (!value_to_key_combos(ctx, &key_combos) || has_key_binding_collisions( - ctx, action, action_map, bindings, &key_combos)) + ctx, action, action_map, bindings, &key_combos, &pipe_argv)) { free_argv(&pipe_argv); free_key_combo_list(&key_combos); return false; } - remove_action_from_key_bindings_list(bindings, action, pipe_argv.args); + remove_action_from_key_bindings_list(bindings, action, &pipe_argv); /* Emit key bindings */ size_t ofs = bindings->count; @@ -2140,11 +2147,10 @@ parse_section_mouse_bindings(struct context *ctx) for (size_t i = 0; i < conf->bindings.mouse.count; i++) { struct config_mouse_binding *binding = &conf->bindings.mouse.arr[i]; - if (binding->action == action && - ((binding->pipe.argv.args == NULL && pipe_argv.args == NULL) || - (binding->pipe.argv.args != NULL && pipe_argv.args != NULL && - argv_compare(binding->pipe.argv.args, pipe_argv.args) == 0))) - { + if (binding->action != action) + continue; + + if (argv_compare(&binding->pipe.argv, &pipe_argv) == 0) { if (binding->pipe.master_copy) free_argv(&binding->pipe.argv); binding->action = BIND_ACTION_NONE; -- cgit v1.3