From 83f21e7cb9846e0e9ed7ab3a2d0757333070fe20 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sun, 18 Oct 2026 02:36:12 +0200 Subject: [PATCH] Fix add overflow panic in table.columnWidth for a huge column index (#922) Found while testing crates/engine/src/cmd/table.rs:540 (column_width) Co-authored-by: Claude Haiku 5.5 --- crates/engine/src/cmd/table.rs | 4 ++-- crates/engine/src/tests.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs index c6728740..8ab95549 100644 --- a/crates/engine/src/cmd/table.rs +++ b/crates/engine/src/cmd/table.rs @@ -537,7 +537,7 @@ fn column_width(s: &mut Session, p: &Value) -> Result { with_table(s, p, "table.columnWidth", move |t| { // Keeping the table's width, the column to the right gives or takes the difference (dragging // an inner border). The last column has none: the table gets wider or narrower. - let pair = t.cols.get(c).zip(t.cols.get(c + 1)).map(|(a, b)| a.max(0.0) + b.max(0.0)).filter(|x| x.is_finite()); + let pair = t.cols.get(c).zip(t.cols.get(c.saturating_add(1))).map(|(a, b)| a.max(0.0) + b.max(0.0)).filter(|x| x.is_finite()); match pair { Some(pair) if keep_total => { let min = MIN_COLUMN_WIDTH.min(pair / 2.0); @@ -545,7 +545,7 @@ fn column_width(s: &mut Session, p: &Value) -> Result { if let Some(x) = t.cols.get_mut(c) { *x = w; } - if let Some(x) = t.cols.get_mut(c + 1) { + if let Some(x) = t.cols.get_mut(c.saturating_add(1)) { *x = pair - w; } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 55bdf4af..a464fd8b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3612,3 +3612,14 @@ fn table_insert_below_or_right_of_a_huge_index_appends_at_the_end() { assert_eq!(t.n_cols(), 3); assert_eq!(t.cell(0, 1).unwrap().text.text(), "b", "the original columns keep their place"); } + +#[test] +fn table_column_width_of_a_huge_column_is_ignored() { + let mut s = session(); + let id = table_at(&mut s, &[100.0, 100.0], json!([["a", "b"], ["c", "d"]])); + for keep_total in [true, false] { + let r = s.execute("table.columnWidth", &json!({"id": id.0, "col": u64::MAX, "width": 50.0, "keepTotal": keep_total})); + assert!(r.is_ok(), "columnWidth must ignore a huge col (keepTotal {keep_total}), got {r:?}"); + assert_eq!(table_of(&s, id).cols, vec![100.0, 100.0], "no column changes"); + } +}