wlr_keyboard: fix mmap leak + logic on close for keymap_fd

mmap leak found through static analysis
This commit is contained in:
Dominique Martinet 2018-06-30 10:41:10 +09:00
parent 266898ca1f
commit efef54ccf5
1 changed files with 10 additions and 3 deletions

View File

@ -144,6 +144,7 @@ void wlr_keyboard_init(struct wlr_keyboard *kb,
wl_signal_init(&kb->events.modifiers); wl_signal_init(&kb->events.modifiers);
wl_signal_init(&kb->events.keymap); wl_signal_init(&kb->events.keymap);
wl_signal_init(&kb->events.repeat_info); wl_signal_init(&kb->events.repeat_info);
kb->keymap_fd = -1;
// Sane defaults // Sane defaults
kb->repeat_info.rate = 25; kb->repeat_info.rate = 25;
@ -156,7 +157,9 @@ void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
} }
xkb_state_unref(kb->xkb_state); xkb_state_unref(kb->xkb_state);
xkb_keymap_unref(kb->keymap); xkb_keymap_unref(kb->keymap);
if (kb->keymap_fd >= 0) {
close(kb->keymap_fd); close(kb->keymap_fd);
}
if (kb->impl && kb->impl->destroy) { if (kb->impl && kb->impl->destroy) {
kb->impl->destroy(kb); kb->impl->destroy(kb);
} else { } else {
@ -212,7 +215,7 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb,
keymap_str = xkb_keymap_get_as_string(kb->keymap, keymap_str = xkb_keymap_get_as_string(kb->keymap,
XKB_KEYMAP_FORMAT_TEXT_V1); XKB_KEYMAP_FORMAT_TEXT_V1);
kb->keymap_size = strlen(keymap_str) + 1; kb->keymap_size = strlen(keymap_str) + 1;
if (kb->keymap_fd) { if (kb->keymap_fd >= 0) {
close(kb->keymap_fd); close(kb->keymap_fd);
} }
kb->keymap_fd = os_create_anonymous_file(kb->keymap_size); kb->keymap_fd = os_create_anonymous_file(kb->keymap_size);
@ -228,6 +231,7 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb,
} }
strcpy(ptr, keymap_str); strcpy(ptr, keymap_str);
free(keymap_str); free(keymap_str);
munmap(ptr, kb->keymap_size);
for (size_t i = 0; i < kb->num_keycodes; ++i) { for (size_t i = 0; i < kb->num_keycodes; ++i) {
xkb_keycode_t keycode = kb->keycodes[i] + 8; xkb_keycode_t keycode = kb->keycodes[i] + 8;
@ -244,7 +248,10 @@ err:
kb->xkb_state = NULL; kb->xkb_state = NULL;
xkb_keymap_unref(keymap); xkb_keymap_unref(keymap);
kb->keymap = NULL; kb->keymap = NULL;
if (kb->keymap_fd >= 0) {
close(kb->keymap_fd); close(kb->keymap_fd);
kb->keymap_fd = -1;
}
free(keymap_str); free(keymap_str);
} }