From 7aadebfe48adfbe87435354b96fc71a63dcf47d1 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 8 Mar 2025 11:07:19 -0700 Subject: [PATCH] termwiz: fix panic when appending long runs to clusterline There were two different overflows that could happen here when resizing the terminal from a narrow width to a much wider width, when the input consists of very very long logical lines. The first was that we could overflow the maximum number of cells in the ClusteredLine struct, which had u16 as its type. The second, after changing u16 to u32, was the maximum length of a run of cells with the same attribute. The former could be fixed, preserving the smaller integer type, by adjusting the API to allow it to return the overflow, but that feels awkward and we could just increase the size of the integer type. The latter can be caught with an overflowing_add operation and breaking the run in the overflow case. That is what this commit does. +bump codec version because this causes a small change in the serialization of line data used by the mux protocol. refs: https://github.com/wezterm/wezterm/issues/6729 --- codec/src/lib.rs | 2 +- docs/changelog.md | 1 + termwiz/src/surface/line/clusterline.rs | 34 +++++++++++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/codec/src/lib.rs b/codec/src/lib.rs index 425f9d329..7869a47bd 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -441,7 +441,7 @@ macro_rules! pdu { /// The overall version of the codec. /// This must be bumped when backwards incompatible changes /// are made to the types and protocol. -pub const CODEC_VERSION: usize = 44; +pub const CODEC_VERSION: usize = 45; // Defines the Pdu enum. // Each struct has an explicit identifying number. diff --git a/docs/changelog.md b/docs/changelog.md index e66e9d9f5..f38275423 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -197,6 +197,7 @@ As features stabilize some brief notes about them will accumulate here. @mgpinf! #6248 #5806 #5346 * Deadlock when a domain detaches due to SSH timeout. Thanks to @joexue! #6749 #6750 +* Panic when rewrapping very very long lines. #6729 #### Updated * Bundled conpty.dll and OpenConsole.exe to build 1.22.250204002.nupkg diff --git a/termwiz/src/surface/line/clusterline.rs b/termwiz/src/surface/line/clusterline.rs index b6631b3b4..a4f523b6d 100644 --- a/termwiz/src/surface/line/clusterline.rs +++ b/termwiz/src/surface/line/clusterline.rs @@ -31,7 +31,7 @@ pub(crate) struct ClusteredLine { is_double_wide: Option>, clusters: Vec, /// Length, measured in cells - len: u16, + len: u32, last_cell_width: Option, } @@ -176,16 +176,25 @@ impl ClusteredLine { } pub fn append_grapheme(&mut self, text: &str, cell_width: usize, attrs: CellAttributes) { + let cell_width = cell_width as u16; let new_cluster = match self.clusters.last() { - Some(cluster) => cluster.attrs != attrs, + Some(cluster) => { + if cluster.attrs != attrs { + true + } else { + // If we overflow the max length of a run, + // then we need a new cluster + let (_, did_overflow) = cluster.cell_width.overflowing_add(cell_width); + did_overflow + } + } None => true, }; let new_cell_index = self.len as usize; - let cell_width = cell_width as u16; if new_cluster { self.clusters.push(Cluster { attrs, cell_width }); } else if let Some(cluster) = self.clusters.last_mut() { - cluster.cell_width += cell_width as u16; + cluster.cell_width += cell_width; } self.text.push_str(text); @@ -205,16 +214,25 @@ impl ClusteredLine { self.is_double_wide.replace(bitset); } self.last_cell_width = NonZeroU8::new(cell_width as u8); - self.len += cell_width; + self.len += cell_width as u32; } pub fn append(&mut self, cell: Cell) { + let cell_width = cell.width() as u16; let new_cluster = match self.clusters.last() { - Some(cluster) => cluster.attrs != *cell.attrs(), + Some(cluster) => { + if cluster.attrs != *cell.attrs() { + true + } else { + // If we overflow the max length of a run, + // then we need a new cluster + let (_, did_overflow) = cluster.cell_width.overflowing_add(cell_width); + did_overflow + } + } None => true, }; let new_cell_index = self.len as usize; - let cell_width = cell.width() as u16; if new_cluster { self.clusters.push(Cluster { attrs: (*cell.attrs()).clone(), @@ -241,7 +259,7 @@ impl ClusteredLine { self.is_double_wide.replace(bitset); } self.last_cell_width = NonZeroU8::new(cell_width as u8); - self.len += cell_width; + self.len += cell_width as u32; } pub fn prune_trailing_blanks(&mut self) -> bool {