Skip to content

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

Fit Artboard on Screen: no i32 overflow for a board with large coordinates (#1181)

wstock64da6bb455e0
diff --git a/crates/ui-egui/src/artboard_ui.rs b/crates/ui-egui/src/artboard_ui.rs
index b12e2a7ee..41f6e7765 100644
--- a/crates/ui-egui/src/artboard_ui.rs
+++ b/crates/ui-egui/src/artboard_ui.rs
@@ -65,7 +65,9 @@ pub fn fit_artboard(app: &mut PhotocraftApp) -> Result<Value, String> {
let v = &mut app.ui.views[i];
// Leave room for the name above the board.
v.zoom = ((area.x - 60.0) / b.width().max(1) as f32).min((area.y - 80.0) / b.height().max(1) as f32).clamp(0.01, 64.0);
- v.center = [(b.x0 + b.x1) as f32 / 2.0, (b.y0 + b.y1) as f32 / 2.0];
+ // Widen before adding: a board near ±2^30, or one whose far edge saturated at i32::MAX,
+ // overflows an i32 sum (#981).
+ v.center = [((f64::from(b.x0) + f64::from(b.x1)) / 2.0) as f32, ((f64::from(b.y0) + f64::from(b.y1)) / 2.0) as f32];
v.fit_pending = false;
Ok(json!({"zoom": v.zoom, "artboard": id.0, "bounds": [b.x0, b.y0, b.x1, b.y1]}))
}
@@ -203,6 +205,26 @@ mod tests {
assert_eq!(app.ui.dock_tabs.history, 2);
}
+ #[test]
+ fn fit_artboard_centres_a_board_with_large_coordinates() {
+ // #981: `(b.x0 + b.x1) as f32` overflowed i32 for boards near ±2^30 or the i32 edges,
+ // panicking outside any dispatch guard.
+ for (params, want) in [
+ (json!({"x": 2147483000, "y": 0, "width": 600, "height": 1000}), [(2147483000.0 + 2147483600.0) / 2.0, 500.0]),
+ (json!({"x": 1073741824, "width": 50, "height": 20}), [1073741849.0, 10.0]),
+ (json!({"y": -1073741840, "width": 50, "height": 20}), [25.0, -1073741830.0]),
+ ] {
+ let (mut app, ctx) = app();
+ let i = app.session.active_index().unwrap();
+ app.run("layer.new.artboard", params.clone()).unwrap();
+ app.sync_views();
+ menu(&mut app, &ctx, "view.fitArtboardOnScreen", json!({})).unwrap();
+ // The view centre is f32, so compare against the true midpoint rounded to f32.
+ let c = app.ui.views[i].center;
+ assert_eq!(c, [want[0] as f32, want[1] as f32], "{params}");
+ }
+ }
+
#[test]
fn comp_target_follows_selection_then_last_applied() {
let (mut app, _) = app();