Skip to content

engine: refuse removeDuplicates with no valid column instead of deleting all rows - #1308

Merged
aiongg merged 1 commit into
storytold:mainfrom
midasmatterhorn:fix/remove-duplicates-empty-columns
Oct 22, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
midasmatterhorn:fix/remove-duplicates-empty-columns

Conversation

@midasmatterhorn

Copy link
Copy Markdown
Contributor

Part of the audit described in the companion issue (findings, reachability arguments, and patches for the remaining defects are filed there).

What this fixes

GridCraft: minimal fix with a regression test, generated from the audit at d9d09f0 and applied on top of current main.

--- a/crates/engine/src/cmd/data.rs
+++ b/crates/engine/src/cmd/data.rs
@@ -748,6 +748,12 @@
         .and_then(Json::as_array)
         .map(|a| a.iter().filter_map(|c| col_param(Some(c), r.start.col)).collect())
         .unwrap_or_else(|| (r.start.col..=r.end.col).collect());
+    // With no column to compare, every row key is empty: the first row would survive and every
+    // other row would be deleted as a duplicate. A column outside the range compares data the
+    // user didn't select. Excel refuses both.
+    if cols.is_empty() || cols.iter().any(|c| !(r.start.col..=r.end.col).contains(c)) {
+        return Err(bad("data.removeDuplicates", "`columns` must name at least one column of the range"));
+    }
     let sheet = d.wb.active_sheet;
     let start = r.start.row + header as u32;
     let mut removed = 0;
@@ -1498,5 +1504,21 @@
         assert_eq!(split_delimited("a,b,,c", &[','], false, '"'), ["a", "b", "", "c"]);
         assert_eq!(split_delimited("a,b,,c", &[','], true, '"'), ["a", "b", "c"]);
         assert_eq!(split_delimited("\"x,y\",z", &[','], false, '"'), ["x,y", "z"]);
+    }
+
+    #[test]
+    fn remove_duplicates_without_a_valid_column_is_refused() {
+        // `columns: []` (or only unusable names) used to compare nothing and delete every row
+        // but the first.
+        let mut s = Session::new();
+        s.new_workbook();
+        s.execute("range.setValues", json!({"range": "A1", "values": [["a"], ["b"], ["c"]]})).unwrap();
+        for columns in [json!([]), json!(["ZZZ"])] {
+            let err = s.execute("data.removeDuplicates", json!({"range": "A1:A3", "columns": columns})).unwrap_err();
+            assert!(err.to_string().contains("columns"), "{err}");
+        }
+        let sh = s.doc().unwrap().wb.active().unwrap();
+        assert_eq!(sh.value(CellRef::new(0, 0)), Value::text("a"));
+        assert_eq!(sh.value(CellRef::new(2, 0)), Value::text("c"));
     }
 }

Verification

Regression test included (remove_duplicates_without_a_valid_column_is_refused), verified locally:

$ cargo test -p gridcraft-engine --lib remove_duplicates
test result: ok. 2 passed; 0 failed

cargo xtask ci should confirm the full gate.

Fixes #1307 (finding 2; the issue carries patches for findings 1, 3, 4).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Audit] 4 defects with evidence + patches: deep formula chain stack overflow, removeDuplicates data loss, clear* changed-cell cap, AutoFill overflow

2 participants