From e81c993ce2e802f67bbcf0d976d3613a9f4b6ecd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 8 Oct 2026 23:30:34 +0530 Subject: [PATCH] Cleanup previous PR Fix dangling text pointer in buffered key events and test CSI param overflow Key events buffered while waiting for an overlay to become ready stored the text pointer from the GLFWkeyevent, which is only valid for the duration of the key callback. Make the buffered events own a copy of the text and free it in a single place. Also add a test for the out of bounds write of is_sub_param when a separator follows the last allowed CSI parameter. --- docs/changelog.rst | 3 +++ kitty/dnd.c | 3 +-- kitty/keys.c | 14 ++++++++++++-- kitty/state.c | 3 +-- kitty/state.h | 1 + kitty_tests/parser.py | 9 +++++++++ 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 64d3810e6..2794d9a25 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -291,6 +291,9 @@ Detailed list of changes a message dragged from Apple Mail, inserting a second path that does not exist (:iss:`10628`) +- Fix text typed into a new window before the program in it is ready sometimes + being garbled + 0.49.2 [2026-10-01] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/dnd.c b/kitty/dnd.c index 81b476c5c..aec24eadb 100644 --- a/kitty/dnd.c +++ b/kitty/dnd.c @@ -2677,8 +2677,7 @@ destroy_fake_window_contents(Window *w) { drag_free_offer(w, false); free(w->pending_clicks.clicks); zero_at_ptr(&w->pending_clicks); - free(w->buffered_keys.key_data); - zero_at_ptr(&w->buffered_keys); + free_buffered_keys(w); Py_CLEAR(w->render_data.screen); Py_CLEAR(w->title); Py_CLEAR(w->title_bar_data.last_drawn_title_object_id); diff --git a/kitty/keys.c b/kitty/keys.c index 20778948e..8e7edf98e 100644 --- a/kitty/keys.c +++ b/kitty/keys.c @@ -244,6 +244,14 @@ dispatch_buffered_keys(Window *w) { debug("Sending previously buffered key "); send_key_to_child(w->id, w->render_data.screen, keys + i); } + free_buffered_keys(w); +} + +void +free_buffered_keys(Window *w) { + GLFWkeyevent *keys = w->buffered_keys.key_data; + // the text of buffered events is owned by us, see on_key_input() + for (size_t i = 0; i < w->buffered_keys.count; i++) free((char *)keys[i].text); free(w->buffered_keys.key_data); zero_at_ptr(&w->buffered_keys); } @@ -351,8 +359,10 @@ on_key_input(const GLFWkeyevent *ev) { if (!new) fatal("Out of memory"); w->buffered_keys.key_data = new; } - GLFWkeyevent *k = w->buffered_keys.key_data; - k[w->buffered_keys.count++] = *ev; + GLFWkeyevent *k = (GLFWkeyevent *)w->buffered_keys.key_data + w->buffered_keys.count++; + *k = *ev; + // ev->text is only valid for the duration of this call + if (ev->text && !(k->text = strdup(ev->text))) fatal("Out of memory"); debug("buffering key until child is ready\n"); } else send_key_to_child(w->id, screen, ev); #undef dispatch_key_event diff --git a/kitty/state.c b/kitty/state.c index eb85a168e..fab63669f 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -432,8 +432,7 @@ destroy_window(Window *w) { drag_free_offer(w, true); free(w->pending_clicks.clicks); zero_at_ptr(&w->pending_clicks); - free(w->buffered_keys.key_data); - zero_at_ptr(&w->buffered_keys); + free_buffered_keys(w); Py_CLEAR(w->render_data.screen); Py_CLEAR(w->title); Py_CLEAR(w->title_bar_data.last_drawn_title_object_id); diff --git a/kitty/state.h b/kitty/state.h index 37c34d3a8..8091a12ca 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -794,6 +794,7 @@ bool render_os_window(OSWindow *w, monotonic_t now, bool scan_for_animated_image void update_mouse_pointer_shape(void); void adjust_window_size_for_csd(OSWindow *w, int width, int height, int *adjusted_width, int *adjusted_height); void dispatch_buffered_keys(Window *w); +void free_buffered_keys(Window *w); bool screen_needs_rendering_in_layers(OSWindow *os_window, Window *w, Screen *screen); void setup_os_window_for_rendering(OSWindow *, Tab *, Window *, bool, monotonic_t); monotonic_t update_custom_shader_animations(unsigned event_mask, monotonic_t now, OSWindow *os_window); diff --git a/kitty_tests/parser.py b/kitty_tests/parser.py index 493a6afa1..bd2353318 100644 --- a/kitty_tests/parser.py +++ b/kitty_tests/parser.py @@ -1020,6 +1020,15 @@ class TestParser(BaseTest): parse_bytes(s, b'\033[?:m\033[31m') self.ae(s.cursor.fg, 1 << 8 | 1) # red (ANSI color 1) + def test_csi_separator_after_max_params(self): + # A ':' or ';' immediately after the last allowed CSI parameter must + # not write past the end of is_sub_param + s = self.create_screen() + pb = partial(self.parse_bytes_dump, s) + too_many = ('CSI escape code has too many parameters, ignoring it',) + pb('\033[' + '1;' * 255 + '1:2m\033[31m', too_many, ('select_graphic_rendition', '31')) + pb('\033[' + '1;' * 256 + 'm\033[31m', ('select_graphic_rendition', ';'.join('1' * 256)), ('select_graphic_rendition', '31')) + def test_csi_code_rep(self): s = self.create_screen(8) pb = partial(self.parse_bytes_dump, s)