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
This commit is contained in:
Wez Furlong committed 2025-03-08 12:25:06 -07:00
1 parent e8d84eeb53
commit 7aadebfe48
3 files changed
+28 -9

No files matched your search

+1 -1
View File
@@ -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.
+1
View File
@@ -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
+26 -8
View File
@@ -31,7 +31,7 @@ pub(crate) struct ClusteredLine {
is_double_wide: Option<Box<FixedBitSet>>,
clusters: Vec<Cluster>,
/// Length, measured in cells
len: u16,
len: u32,
last_cell_width: Option<NonZeroU8>,
}
@@ -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 {