From 675480105270eb095a9d3178aed345712927e8ff Mon Sep 17 00:00:00 2001 From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Fri, 15 Nov 2024 22:58:44 +0100 Subject: [PATCH] rcxml.c: fix mem leak when deduplicating keybinds Before this patch `keybind->keysyms` wasn't free'd when - deduplicating keybinds - removing keybinds due to empty action list This patch creates a shared `keybind_destroy()` helper which gets used in all cases where a keybind is destroyed. --- include/config/keybind.h | 2 ++ src/config/keybind.c | 9 +++++++++ src/config/rcxml.c | 7 +++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/config/keybind.h b/include/config/keybind.h index 36d81373..386839ee 100644 --- a/include/config/keybind.h +++ b/include/config/keybind.h @@ -30,6 +30,8 @@ struct keybind { */ struct keybind *keybind_create(const char *keybind); +void keybind_destroy(struct keybind *keybind); + /** * parse_modifier - parse a string containing a single modifier name (e.g. "S") * into the represented modifier value. returns 0 for invalid modifier names. diff --git a/src/config/keybind.c b/src/config/keybind.c index 3370c09d..5e5002f7 100644 --- a/src/config/keybind.c +++ b/src/config/keybind.c @@ -188,3 +188,12 @@ keybind_create(const char *keybind) wl_list_init(&k->actions); return k; } + +void +keybind_destroy(struct keybind *keybind) +{ + assert(wl_list_empty(&keybind->actions)); + + zfree(keybind->keysyms); + zfree(keybind); +} diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 3951c72f..ce77c8c0 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -1626,7 +1626,7 @@ deduplicate_key_bindings(void) if (keybind_the_same(existing, current)) { wl_list_remove(&existing->link); action_list_free(&existing->actions); - free(existing); + keybind_destroy(existing); replaced++; break; } @@ -1635,7 +1635,7 @@ deduplicate_key_bindings(void) wl_list_for_each_safe(current, tmp, &rc.keybinds, link) { if (wl_list_empty(¤t->actions)) { wl_list_remove(¤t->link); - free(current); + keybind_destroy(current); cleared++; } } @@ -1948,8 +1948,7 @@ rcxml_finish(void) wl_list_for_each_safe(k, k_tmp, &rc.keybinds, link) { wl_list_remove(&k->link); action_list_free(&k->actions); - zfree(k->keysyms); - zfree(k); + keybind_destroy(k); } struct mousebind *m, *m_tmp;