Skip to content

Commit 0ce545f

Browse files
Reject invalid row/col index in table row and column commands
Found while testing crates/engine/src/cmd/table.rs:234-238 (at), via crates/engine/src/cmd/mod.rs:318 (usize_param) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent a243538 commit 0ce545f

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎crates/engine/src/cmd/table.rs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ fn cell_param(p: &Value, key: &str) -> Option<(usize, usize)> {
231231
}
232232

233233
/// The table's row/col to act on: params, the edited cell, or the selected cells.
234-
fn at(s: &Session, p: &Value) -> (usize, usize) {
234+
/// A present but invalid `row`/`col` is an error; only an absent one falls back.
235+
fn at(s: &Session, p: &Value, cmd: &str) -> Result<(usize, usize)> {
235236
let st = s.active();
236237
let cell = st.and_then(|d| d.selection.text.as_ref().and_then(|t| t.cell)).or_else(|| st.and_then(|d| d.selection.cells.map(|c| c.1)));
237-
(usize_param(p, "row").or(cell.map(|c| c.0)).unwrap_or(0), usize_param(p, "col").or(cell.map(|c| c.1)).unwrap_or(0))
238+
Ok((opt_usize_param(p, "row", cmd)?.or(cell.map(|c| c.0)).unwrap_or(0), opt_usize_param(p, "col", cmd)?.or(cell.map(|c| c.1)).unwrap_or(0)))
238239
}
239240

240241
fn with_table(s: &mut Session, p: &Value, cmd: &str, f: impl Fn(&mut Table) -> Result<()>) -> Result<Value> {
@@ -283,7 +284,7 @@ fn with_table(s: &mut Session, p: &Value, cmd: &str, f: impl Fn(&mut Table) -> R
283284
}
284285

285286
fn rows_cols(s: &mut Session, p: &Value, op: &str) -> Result<Value> {
286-
let (r, c) = at(s, p);
287+
let (r, c) = at(s, p, &format!("table.{op}"))?;
287288
let op = op.to_string();
288289
with_table(s, p, "table", move |t| {
289290
match op.as_str() {
@@ -319,7 +320,10 @@ fn merge(s: &mut Session, p: &Value) -> Result<Value> {
319320
}
320321

321322
fn split(s: &mut Session, p: &Value) -> Result<Value> {
322-
let cell = cell_param(p, "cell").unwrap_or_else(|| at(s, p));
323+
let cell = match cell_param(p, "cell") {
324+
Some(c) => c,
325+
None => at(s, p, "table.split")?,
326+
};
323327
with_table(s, p, "table.split", move |t| if t.split(cell.0, cell.1) { Ok(()) } else { Err(bad("table.split", "that cell isn't merged")) })
324328
}
325329

@@ -531,7 +535,7 @@ fn distribute(s: &mut Session, p: &Value, rows: bool) -> Result<Value> {
531535
}
532536

533537
fn column_width(s: &mut Session, p: &Value) -> Result<Value> {
534-
let (_, c) = at(s, p);
538+
let (_, c) = at(s, p, "table.columnWidth")?;
535539
let w = f64_param(p, "width").ok_or_else(|| bad("table.columnWidth", "missing `width`"))?.clamp(4.0, 4000.0);
536540
let keep_total = bool_or(p, "keepTotal", false);
537541
with_table(s, p, "table.columnWidth", move |t| {
@@ -560,7 +564,7 @@ fn column_width(s: &mut Session, p: &Value) -> Result<Value> {
560564
}
561565

562566
fn row_height(s: &mut Session, p: &Value) -> Result<Value> {
563-
let (r, _) = at(s, p);
567+
let (r, _) = at(s, p, "table.rowHeight")?;
564568
let h = f64_param(p, "height").ok_or_else(|| bad("table.rowHeight", "missing `height`"))?.clamp(4.0, 4000.0);
565569
with_table(s, p, "table.rowHeight", move |t| {
566570
if let Some(x) = t.rows.get_mut(r) {

‎crates/engine/src/tests.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,26 @@ fn table_insert_below_or_right_of_a_huge_index_appends_at_the_end() {
36723672
assert_eq!(t.cell(0, 1).unwrap().text.text(), "b", "the original columns keep their place");
36733673
}
36743674

3675+
#[test]
3676+
fn table_row_col_commands_reject_an_invalid_index() {
3677+
let mut s = session();
3678+
let id = table_at(&mut s, &[100.0, 100.0], json!([["a", "b"], ["c", "d"], ["e", "f"]]));
3679+
for (cmd, key) in [
3680+
("table.deleteRow", "row"),
3681+
("table.deleteColumn", "col"),
3682+
("table.insertRowAbove", "row"),
3683+
("table.insertColumnLeft", "col"),
3684+
("table.split", "row"),
3685+
] {
3686+
for bad in [json!(-1), json!(1.5), json!("1")] {
3687+
let r = s.execute(cmd, &json!({"id": id.0, key: bad}));
3688+
assert!(r.is_err(), "{cmd} {key}={bad} should be rejected, got {r:?}");
3689+
let t = table_of(&s, id);
3690+
assert_eq!((t.n_rows(), t.n_cols()), (3, 2), "{cmd} {key}={bad} must leave the table unchanged");
3691+
}
3692+
}
3693+
}
3694+
36753695
#[test]
36763696
fn table_column_width_of_a_huge_column_is_ignored() {
36773697
let mut s = session();

0 commit comments

Comments
 (0)