Fix cursor underflow when VS15 narrows a wide char on the previous soft wrapped line

The VS15 branch of draw_combining_char unconditionally moved the cursor
back by the width removed from the char. When the char was on the
previous line (cursor at x=0 after a soft wrap) this wrapped cursor->x to
UINT_MAX leading to out of bounds reads and writes. Only move the cursor
when it is on the same line as the char. Also preserve the soft wrap
marker in the cells cleared by halve_multicell_width.
This commit is contained in:
Kovid Goyal 2026-11-15 08:16:13 +05:30
parent 8e5487e9d4
commit 64a02b4061
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 50 additions and 3 deletions

View file

@ -133,6 +133,8 @@ Detailed list of changes
:ac:`resize_window` action and :code:`kitten @ resize-window`, see :ref:`docks`
(:iss:`10857`)
- Fix a crash when the VS15 variation selector is received with the cursor at the start of a line, after a wide emoji that was soft wrapped from the previous line
- Unicode input kitten: Fix searching by name not finding some characters such as the pound sign (:iss:`10862`)
- choose-files kitten: Sanitize file names, archive member names, ebook metadata and previewer output everywhere they are displayed so they cannot inject escape codes into the terminal

View file

@ -1210,7 +1210,8 @@ halve_multicell_width(Screen *self, index_type x_, index_type y_) {
GPUCell *g = line->gpu_cells;
for (index_type x = x_; x < half_x_limit; x++) c[x].width = new_width;
for (index_type x = half_x_limit; x < x_limit; x++) {
c[x] = (CPUCell){0};
// preserve the soft wrap marker, which lives in the last cell of the line
c[x] = (CPUCell){.next_char_was_wrapped = c[x].next_char_was_wrapped};
clear_sprite_position(g[x]);
}
mark_range_line_dirty(self, y);
@ -1429,9 +1430,16 @@ draw_combining_char(Screen *self, text_loop_state *s, char_type ch) {
// matching how VS16 and Thai/Lao SARA AM leave such cells alone.
if (self->lc->chars[base_pos + 1] == VS15 && cpu_cell->is_multicell && cpu_cell->natural_width && cpu_cell->width == 2 &&
is_emoji_presentation_base(self->lc->chars[base_pos])) {
index_type deltax = (cpu_cell->scale * cpu_cell->width) / 2;
const index_type old_limit = xpos + cpu_cell->scale * cpu_cell->width;
const index_type new_limit = old_limit - (cpu_cell->scale * cpu_cell->width) / 2;
halve_multicell_width(self, xpos, s->prev.y);
self->cursor->x -= deltax;
// The cursor only needs to move if it is on the same line as the
// narrowed char, the char can also be on the previous line when the
// cursor is at the start of a line that the char soft wrapped onto.
if (self->cursor->y == s->prev.y && self->cursor->x > new_limit) {
if (self->cursor->x >= old_limit) self->cursor->x -= old_limit - new_limit;
else self->cursor->x = new_limit;
}
init_segmentation_state(self, s);
}
} else if (s->seg.grapheme_break == GBP_SpacingMark && !cp[xpos].is_multicell && wcwidth_std(char_props_for(ch)) > 0) {

View file

@ -1687,6 +1687,43 @@ class TestScreen(BaseTest):
t('\u25b6', 1, '\ufe0f', 2, '\ufe0e', 2)
# wide + VS15 + VS16
t('\U0001f610', 2, '\ufe0e', 1, '\ufe0f', 1)
# VS15 attaching to a wide char on the previous, soft wrapped line
s = self.create_screen(cols=5, lines=5)
parse_bytes(s, 'abc\U0001f610f\r'.encode())
self.ae((s.cursor.x, s.cursor.y), (0, 1))
parse_bytes(s, '\ufe0e'.encode())
self.ae((s.cursor.x, s.cursor.y), (0, 1))
self.ae(s.cpu_cells(0, 3)['mcd']['width'], 1)
self.assertTrue(s.cpu_cells(0, 4)['next_char_was_wrapped'])
parse_bytes(s, b'gh')
self.ae((s.cursor.x, s.cursor.y), (2, 1))
self.ae(str(s.line(0)), 'abc\U0001f610\ufe0e')
self.ae(str(s.line(1)), 'gh')
# scaled natural width char narrowed by VS15, on the same line and soft wrapped
s = self.create_screen(cols=8, lines=5)
parse_bytes(s, b'ab')
draw_multicell(s, '\U0001f610', scale=2)
self.ae(s.cursor.x, 6)
parse_bytes(s, '\ufe0e'.encode())
self.ae(s.cursor.x, 4)
for y in (0, 1):
self.ae(s.cpu_cells(y, 2)['mcd']['width'], 1)
s = self.create_screen(cols=6, lines=5)
parse_bytes(s, b'ab')
draw_multicell(s, '\U0001f610', scale=2)
parse_bytes(s, b'f\r')
self.ae((s.cursor.x, s.cursor.y), (0, 1))
parse_bytes(s, '\ufe0e'.encode())
self.ae((s.cursor.x, s.cursor.y), (0, 1))
self.ae(s.cpu_cells(0, 2)['mcd']['width'], 1)
self.assertTrue(s.cpu_cells(0, 5)['next_char_was_wrapped'])
# cursor inside the char is clamped to the end of the narrowed char
s = self.create_screen(cols=8, lines=5)
parse_bytes(s, b'ab')
draw_multicell(s, '\U0001f610', scale=2)
parse_bytes(s, b'\x1b[1;6H\xef\xb8\x8e')
self.ae(s.cursor.x, 4)
s = self.create_screen()
# large numbers of combining chars
s.reset()
s.draw('\N{HEAVY EXCLAMATION MARK SYMBOL}' + 4500 * '\N{VARIATION SELECTOR-16}')