PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(engine): divide wide slice rectangles without integer overflow (#991)
Nigel Ihza Firdausybdff608a0c57
diff --git a/crates/engine/src/slice_cmds.rs b/crates/engine/src/slice_cmds.rsindex 8854e4383..f632736a8 100644--- a/crates/engine/src/slice_cmds.rs+++ b/crates/engine/src/slice_cmds.rs@@ -390,7 +390,7 @@ fn divide(s: &mut Session, p: &Value) -> Result<Value> {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"))?,};- if (r.width() as i32) < across || (r.height() as i32) < down {+ if r.width() < across as u32 || r.height() < down as u32 {return Err(bad(cmd, "the slice is too small to divide that many times"));}let n = (down * across) as usize;@@ -405,10 +405,11 @@ fn divide(s: &mut Session, p: &Value) -> Result<Value> {let mut next_id = doc.slices.next_id();for j in 0..down {for i in 0..across {- let x0 = r.x0 + (r.width() as i32 * i) / across;- let x1 = r.x0 + (r.width() as i32 * (i + 1)) / across;- let y0 = r.y0 + (r.height() as i32 * j) / down;- let y1 = r.y0 + (r.height() as i32 * (j + 1)) / down;+ // Products can exceed i32 even when every interpolated edge fits the rect.+ let x = |k: i32| (i64::from(r.x0) + i64::from(r.width()) * i64::from(k) / i64::from(across)) as i32;+ let y = |k: i32| (i64::from(r.y0) + i64::from(r.height()) * i64::from(k) / i64::from(down)) as i32;+ let (x0, x1) = (x(i), x(i + 1));+ let (y0, y1) = (y(j), y(j + 1));let nid = if ids.is_empty() {id} else {diff --git a/crates/engine/src/slice_cmds/tests.rs b/crates/engine/src/slice_cmds/tests.rsindex cee4ef529..25fc9a7ab 100644--- a/crates/engine/src/slice_cmds/tests.rs+++ b/crates/engine/src/slice_cmds/tests.rs@@ -84,6 +84,50 @@ fn promote_auto_slice_and_divide() {assert!(s.execute("slice.divide", json!({"slice": id})).is_err());}+#[test]+fn divide_wide_slice_preserves_exact_edges_and_history() {+ for depth in [8, 16, 32] {+ for rect in+ [Rect::new(-2_000_000_000, -1_000_000_000, 2_000_000_000, 1_000_000_000), Rect::new(-1_000_000_000, -1_000_000_000, 1_000_000_000, 1_000_000_000)]+ {+ let mut s = session(depth);+ s.edit("loaded wide slice", |doc, _| {+ doc.slices.list.push(Slice { id: 1, rect, name: "wide".into(), ..Default::default() });+ Ok(())+ })+ .unwrap();+ let before = doc(&s).slices.clone();+ let past = s.active().unwrap().history.past_len();+ let result = s.execute("slice.divide", json!({"slice": 1, "horizontal": 3, "vertical": 3})).unwrap();+ assert_eq!(result["count"], 9);+ assert_eq!(s.active().unwrap().history.past_len(), past + 1);+ let parts = &doc(&s).slices.list;+ assert_eq!(parts.len(), 9);+ assert_eq!(parts[0].name, "wide");+ for row in parts.chunks_exact(3) {+ assert_eq!(row[0].rect.x0, rect.x0);+ assert_eq!(row[2].rect.x1, rect.x1);+ assert_eq!(row.iter().map(|s| u64::from(s.rect.width())).sum::<u64>(), u64::from(rect.width()));+ for pair in row.windows(2) {+ assert_eq!(pair[0].rect.x1, pair[1].rect.x0);+ assert_eq!(pair[0].rect.y0, pair[1].rect.y0);+ assert_eq!(pair[0].rect.y1, pair[1].rect.y1);+ }+ }+ assert_eq!(parts[0].rect.y0, rect.y0);+ assert_eq!(parts[8].rect.y1, rect.y1);+ for i in 0..6 {+ assert_eq!(parts[i].rect.y1, parts[i + 3].rect.y0);+ }+ let divided = doc(&s).slices.clone();+ assert!(s.undo());+ assert_eq!(doc(&s).slices, before);+ assert!(s.redo());+ assert_eq!(doc(&s).slices, divided);+ }+ }+}+#[test]fn slices_from_guides_and_clear() {let mut s = session(8);