Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Divide Slice: one division makes at most 10000 parts (#1020) (#1504)

kayrockscreenprintinge486262d3e17
diff --git a/crates/engine/src/slice_cmds.rs b/crates/engine/src/slice_cmds.rs
index e0bf9df37..386ccd136 100644
--- a/crates/engine/src/slice_cmds.rs
+++ b/crates/engine/src/slice_cmds.rs
@@ -378,6 +378,11 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"deleted": n}))
}
+/// The most parts one Divide Slice makes. Resolving the slice list is quadratic in the slice count
+/// (`slices::resolve`), so a 1000 x 1000 division, a million slices, left every later list or
+/// export with ~5e11 steps of work (#1020).
+const MAX_DIVIDE_PARTS: i32 = 10_000;
+
fn divide(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "slice.divide";
let t = find(s, p, cmd)?;
@@ -386,6 +391,9 @@ fn divide(s: &mut Session, p: &Value) -> Result<Value> {
if down == 1 && across == 1 {
return Err(bad(cmd, "give \"horizontal\" (slices down) and/or \"vertical\" (slices across) > 1"));
}
+ if down * across > MAX_DIVIDE_PARTS {
+ return Err(bad(cmd, format!("{down} x {across} = {} parts is more than one division makes ({MAX_DIVIDE_PARTS})", down * across)));
+ }
let r = match t {
Target::Auto(r) => r,
Target::Stored(id) => s.active().and_then(|d| d.doc.slices.get(id)).map(|sl| sl.rect).ok_or_else(|| bad(cmd, "no such slice"))?,
@@ -517,7 +525,7 @@ pub fn specs() -> Vec<CommandSpec> {
"slice.divide",
"Divide Slice…",
&[],
- r##"{"slice":id | "number":n,"horizontal":n=1 (slices down),"vertical":n=1 (slices across)} → {slices}"##,
+ r##"{"slice":id | "number":n,"horizontal":n=1 (slices down),"vertical":n=1 (slices across); at most 10000 parts} → {slices}"##,
unlocked,
divide
),
diff --git a/crates/engine/src/slice_cmds/tests.rs b/crates/engine/src/slice_cmds/tests.rs
index 70b50bc64..4c3008ef4 100644
--- a/crates/engine/src/slice_cmds/tests.rs
+++ b/crates/engine/src/slice_cmds/tests.rs
@@ -325,3 +325,24 @@ fn out_of_range_slice_ids_are_rejected_not_wrapped() {
assert_eq!(doc(&s).slices.list.len(), 1);
assert_eq!(doc(&s).slices.list[0].name, "");
}
+
+#[test]
+fn divide_refuses_more_parts_than_one_division_makes() {
+ // #1020: 1000 x 1000 was accepted (a million slices), and resolving the list is quadratic in
+ // the slice count.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 1000, "height": 1000})).unwrap();
+ s.execute("slice.new", json!({"rect": [0, 0, 1000, 1000]})).unwrap();
+ let id = doc(&s).slices.list[0].id;
+ for (h, v) in [(101, 100), (100, 101), (1000, 1000)] {
+ let (before, past) = (doc(&s).slices.clone(), s.active().unwrap().history.past_len());
+ let err = s.execute("slice.divide", json!({"slice": id, "horizontal": h, "vertical": v})).unwrap_err();
+ assert!(err.to_string().contains("10000"), "{h} x {v}: {err}");
+ assert_eq!((&doc(&s).slices, s.active().unwrap().history.past_len()), (&before, past), "{h} x {v}: nothing changed");
+ }
+ // At the limit it divides, and the list resolves.
+ let r = s.execute("slice.divide", json!({"slice": id, "horizontal": 100, "vertical": 100})).unwrap();
+ assert_eq!(r["count"], 10_000);
+ assert_eq!(doc(&s).slices.list.len(), 10_000);
+ assert!(s.execute("slice.list", json!({})).is_ok());
+}