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.
This commit is contained in:
Kovid Goyal 2026-10-08 23:30:34 +05:30
parent 390306edcc
commit e81c993ce2
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
6 changed files with 27 additions and 6 deletions

View file

@ -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]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -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);

View file

@ -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

View file

@ -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);

View file

@ -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);

View file

@ -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)