From 1bb408462c5cbbbc218c6923601d98071e91846f Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Fri, 16 Oct 2026 04:09:45 +0000 Subject: [PATCH] Clamp huge index in table.insertRowBelow/insertColumnRight Found while testing crates/engine/src/cmd/table.rs:291 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/engine/src/cmd/table.rs | 4 ++-- crates/engine/src/tests.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs index 1c5b080c..c6728740 100644 --- a/crates/engine/src/cmd/table.rs +++ b/crates/engine/src/cmd/table.rs @@ -288,9 +288,9 @@ fn rows_cols(s: &mut Session, p: &Value, op: &str) -> Result { with_table(s, p, "table", move |t| { match op.as_str() { "rowAbove" => t.insert_row(r), - "rowBelow" => t.insert_row(r + 1), + "rowBelow" => t.insert_row(r.saturating_add(1)), "colLeft" => t.insert_col(c), - "colRight" => t.insert_col(c + 1), + "colRight" => t.insert_col(c.saturating_add(1)), "delRow" => { if !t.delete_row(r) { return Err(bad("table.deleteRow", "can't delete the last row")); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index a333cb86..f7022eab 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3576,3 +3576,19 @@ fn shape_ids_beyond_u32_are_rejected_not_wrapped() { assert!(s.execute("shape.sites", &json!({"id": a.0})).is_ok()); assert!(s.execute("shape.connect", &json!({"from": a.0, "to": b.0})).is_ok()); } + +#[test] +fn table_insert_below_or_right_of_a_huge_index_appends_at_the_end() { + let mut s = session(); + let id = table_at(&mut s, &[100.0, 100.0], json!([["a", "b"], ["c", "d"]])); + let r = s.execute("table.insertRowBelow", &json!({"id": id.0, "row": u64::MAX})); + assert!(r.is_ok(), "insertRowBelow must clamp a huge row, got {r:?}"); + let t = table_of(&s, id); + assert_eq!(t.n_rows(), 3); + assert_eq!(t.cell(0, 0).unwrap().text.text(), "a", "the original rows keep their place"); + let r = s.execute("table.insertColumnRight", &json!({"id": id.0, "col": u64::MAX})); + assert!(r.is_ok(), "insertColumnRight must clamp a huge col, got {r:?}"); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 3); + assert_eq!(t.cell(0, 1).unwrap().text.text(), "b", "the original columns keep their place"); +}