mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
slice.divide clamped each axis to 1..1000 but checked no total, so one request made up to a million slices, and resolving the slice list (slices::resolve, used by slice.list and export) is quadratic in the slice count: about 5e11 rectangle visits at a million. A division over 10000 parts (100 x 100) is now refused with a bad-params error before any part is created; the params doc says so. Resolving 10000 slices is about 5e7 visits, and the test lists them in well under a second. The test refuses 101 x 100, 100 x 101 and 1000 x 1000 with the slices and history unchanged, then divides 100 x 100 into 10000 parts and lists them. Before the fix 101 x 100 was accepted (10100 parts). Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
62fd1342dd
commit
e486262d3e
2 files changed
+30
-1
No files matched your search
@@ -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
|
||||
),
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
Reference in new issue
Block a user