From ff153ba27f3aeb75307ddb3ffd7e3eae0567bb20 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 8 May 2021 00:39:29 -0700 Subject: [PATCH] vtparse: recognize utf8 encoded c1 codes in more cases There were two bugs here: * \u8D (the utf8 encoded representation of 0x8d, aka: RI) was not recognized as a C1 code and was instead passed through as printable text. * The \u8D is a zero-width sequence which means that a subsequent set_cell call on the new empty-by-default line wouldn't allocate any cells in the line array, and the assigment to the line would panic. This commit avoids the panic for the second case, and then fixes up the vtparser to correctly recognize the sequence as a C1 control. refs: https://github.com/wez/wezterm/issues/768 --- docs/changelog.md | 1 + term/src/test/mod.rs | 7 +++++++ termwiz/src/surface/line.rs | 12 +++++++++--- vtparse/src/lib.rs | 12 +++++++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 7e54a77d9..4bf39326f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -13,6 +13,7 @@ As features stabilize some brief notes about them will accumulate here. * Fixed: ssh client would read `/etc/ssh/config` rather than the proper `/etc/ssh/ssh_config` * x11: support for [VoidSymbol](config/keys.md#voidsymbol) in key assignments. Thanks to [@digitallyserviced](https://github.com/digitallyserviced)! [#759](https://github.com/wez/wezterm/pull/759) +* Fixed: UTF8-encoded-C1 control codes were not always recognized as control codes, and could result in a panic when later attempting to update the line. [#768](https://github.com/wez/wezterm/issues/768) ### 20210502-154244-3f7122cb diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs index 1b924e945..7df1b4c66 100644 --- a/term/src/test/mod.rs +++ b/term/src/test/mod.rs @@ -789,6 +789,13 @@ fn test_scrollup() { assert_eq!(term.screen().visible_row_to_stable_row(0), 7); } +#[test] +fn test_ri() { + let mut term = TestTerm::new(3, 1, 10); + term.print("1\n\u{8d}\n"); + assert_all_contents(&term, file!(), line!(), &["1", "", ""]); +} + #[test] fn test_scroll_margins() { let mut term = TestTerm::new(3, 1, 10); diff --git a/termwiz/src/surface/line.rs b/termwiz/src/surface/line.rs index a0c0b039e..3076309de 100644 --- a/termwiz/src/surface/line.rs +++ b/termwiz/src/surface/line.rs @@ -305,9 +305,15 @@ impl Line { pub fn set_cell(&mut self, idx: usize, cell: Cell) -> &Cell { let width = cell.width(); - // if the line isn't wide enough, pad it out with the default attributes - if idx + width >= self.cells.len() { - self.cells.resize(idx + width, Cell::default()); + // if the line isn't wide enough, pad it out with the default attributes. + // The .max(1) stuff is here in case we get called with a + // zero-width cell. That shouldn't happen: those sequences + // should get filtered out in the terminal parsing layer, + // but in case one does sneak through, we need to ensure that + // we grow the cells array to hold this bogus entry. + // https://github.com/wez/wezterm/issues/768 + if idx + width.max(1) >= self.cells.len() { + self.cells.resize(idx + width.max(1), Cell::default()); } self.invalidate_implicit_hyperlinks(); diff --git a/vtparse/src/lib.rs b/vtparse/src/lib.rs index 9c62c0922..cb0a02df5 100644 --- a/vtparse/src/lib.rs +++ b/vtparse/src/lib.rs @@ -622,7 +622,9 @@ impl VTParser { let byte = ((c as u32) & 0xff) as u8; let (action, state) = lookup(self.utf8_return_state, byte); - if state != self.utf8_return_state && state != State::Utf8Sequence { + if action == Action::Execute + || (state != self.utf8_return_state && state != State::Utf8Sequence) + { self.action(lookup_exit(self.utf8_return_state), 0, actor); self.action(action, byte, actor); self.action(lookup_entry(state), 0, actor); @@ -941,6 +943,14 @@ mod test { ); } + #[test] + fn utf8_control() { + assert_eq!( + parse_as_vec("\u{8d}".as_bytes()), + vec![VTAction::ExecuteC0orC1(0x8d)] + ); + } + #[test] fn tmux_control() { assert_eq!(