From: Elouan Martinet Date: Tue, 20 Mar 2018 13:06:17 +0000 (+0000) Subject: Fix potential memory leak X-Git-Tag: 4.16~111^2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=e6bd2006bcbdd39d23cc43051de7743708ca43c3;p=i3%2Fi3 Fix potential memory leak xkb_state_new uses calloc and may fail in a rare case, which would cause a memory leak. Note that xkb_state_unref checks if the parameter given is not null (!state) before freeing. Calls to xkb_state_new have been grouped to remove code duplication. Signed-off-by: Elouan Martinet --- diff --git a/src/bindings.c b/src/bindings.c index c145b956..fe77bc8f 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -431,31 +431,20 @@ static void add_keycode_if_matches(struct xkb_keymap *keymap, xkb_keycode_t key, * */ void translate_keysyms(void) { - struct xkb_state *dummy_state = xkb_state_new(xkb_keymap); - if (dummy_state == NULL) { - ELOG("Could not create XKB state, cannot translate keysyms.\n"); - return; - } - - struct xkb_state *dummy_state_no_shift = xkb_state_new(xkb_keymap); - if (dummy_state_no_shift == NULL) { - ELOG("Could not create XKB state, cannot translate keysyms.\n"); - return; - } - - struct xkb_state *dummy_state_numlock = xkb_state_new(xkb_keymap); - if (dummy_state_numlock == NULL) { - ELOG("Could not create XKB state, cannot translate keysyms.\n"); - return; - } + struct xkb_state *dummy_state = NULL; + struct xkb_state *dummy_state_no_shift = NULL; + struct xkb_state *dummy_state_numlock = NULL; + struct xkb_state *dummy_state_numlock_no_shift = NULL; + bool has_errors = false; - struct xkb_state *dummy_state_numlock_no_shift = xkb_state_new(xkb_keymap); - if (dummy_state_numlock_no_shift == NULL) { + if ((dummy_state = xkb_state_new(xkb_keymap)) == NULL || + (dummy_state_no_shift = xkb_state_new(xkb_keymap)) == NULL || + (dummy_state_numlock = xkb_state_new(xkb_keymap)) == NULL || + (dummy_state_numlock_no_shift = xkb_state_new(xkb_keymap)) == NULL) { ELOG("Could not create XKB state, cannot translate keysyms.\n"); - return; + goto out; } - bool has_errors = false; Binding *bind; TAILQ_FOREACH(bind, bindings, bindings) { #define ADD_TRANSLATED_KEY(code, mods) \ @@ -620,6 +609,7 @@ void translate_keysyms(void) { #undef ADD_TRANSLATED_KEY } +out: xkb_state_unref(dummy_state); xkb_state_unref(dummy_state_no_shift); xkb_state_unref(dummy_state_numlock);