From f9a6e265e9727abed3f6f6c57fc17c53068f17dd Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 8 May 2021 01:02:44 -0700 Subject: [PATCH] avoid panics in a couple of cases test case: `cat /dev/random` refs: https://github.com/wez/wezterm/issues/769 --- term/src/terminalstate.rs | 26 +++++++++++++++----------- termwiz/src/surface/line.rs | 6 ++++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index d6b2740e5..10ec961d4 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -2361,18 +2361,22 @@ impl TerminalState { let line_idx = screen.phys_row(y); let line = screen.line_mut(line_idx); - let candidate = &line.cells()[to_copy]; - if candidate.str() == " " && to_copy > 0 { - // It's a blank. It may be the second part of - // a double-wide pair; look ahead of it. - let prior = &line.cells()[to_copy - 1]; - if prior.width() > 1 { - prior.clone() - } else { - candidate.clone() + match line.cells().get(to_copy).cloned() { + None => Cell::new(' ', CellAttributes::default()), + Some(candidate) => { + if candidate.str() == " " && to_copy > 0 { + // It's a blank. It may be the second part of + // a double-wide pair; look ahead of it. + let prior = &line.cells()[to_copy - 1]; + if prior.width() > 1 { + prior.clone() + } else { + candidate + } + } else { + candidate + } } - } else { - candidate.clone() } }; diff --git a/termwiz/src/surface/line.rs b/termwiz/src/surface/line.rs index 3076309de..ab391dd4e 100644 --- a/termwiz/src/surface/line.rs +++ b/termwiz/src/surface/line.rs @@ -399,8 +399,10 @@ impl Line { pub fn erase_cell_with_margin(&mut self, x: usize, right_margin: usize) { self.invalidate_implicit_hyperlinks(); - self.invalidate_grapheme_at_or_before(x); - self.cells.remove(x); + if x < self.cells.len() { + self.invalidate_grapheme_at_or_before(x); + self.cells.remove(x); + } if right_margin <= self.cells.len() { self.cells.insert(right_margin - 1, Cell::default()); }