diff --git a/crates/engine/src/cmd/shape.rs b/crates/engine/src/cmd/shape.rs index 51b1a680..f1a050d1 100644 --- a/crates/engine/src/cmd/shape.rs +++ b/crates/engine/src/cmd/shape.rs @@ -169,13 +169,24 @@ pub fn specs() -> Vec { ] } +/// Keeps a side length in `0..=MAX_PT` so a huge value cannot exhaust memory when rendering. +/// An overflow to infinity lands on the limit; NaN becomes 0. +fn clamp_side(v: f64) -> f64 { + if v.is_nan() { 0.0 } else { v.clamp(0.0, MAX_PT) } +} + +/// Keeps a coordinate within `±POS_LIMIT`. +fn clamp_pos(v: f64) -> f64 { + if v.is_nan() { 0.0 } else { v.clamp(-POS_LIMIT, POS_LIMIT) } +} + fn move_by(s: &mut Session, p: &Value) -> Result { let (dx, dy) = (f64_param(p, "dx"), f64_param(p, "dy")); let (ax, ay) = (f64_param(p, "x"), f64_param(p, "y")); edit_geometry(s, p, "shape.move", |sh| { if let Some(x) = sh.xfrm.as_mut() { - x.x = ax.unwrap_or(x.x) + dx.unwrap_or(0.0); - x.y = ay.unwrap_or(x.y) + dy.unwrap_or(0.0); + x.x = clamp_pos(ax.unwrap_or(x.x) + dx.unwrap_or(0.0)); + x.y = clamp_pos(ay.unwrap_or(x.y) + dy.unwrap_or(0.0)); } Ok(()) }) @@ -202,10 +213,12 @@ fn resize(s: &mut Session, p: &Value) -> Result { x.h = h.unwrap_or(x.h); } } + x.w = clamp_side(x.w); + x.h = clamp_side(x.h); // Rotated shapes resize about their centre; others keep the top-left. if x.rot != 0.0 { - x.x = cx - x.w / 2.0; - x.y = cy - x.h / 2.0; + x.x = clamp_pos(cx - x.w / 2.0); + x.y = clamp_pos(cy - x.h / 2.0); } } Ok(()) @@ -215,10 +228,10 @@ fn resize(s: &mut Session, p: &Value) -> Result { fn set_bounds(s: &mut Session, p: &Value) -> Result { edit_geometry(s, p, "shape.setBounds", |sh| { if let Some(x) = sh.xfrm.as_mut() { - x.x = f64_param(p, "x").unwrap_or(x.x); - x.y = f64_param(p, "y").unwrap_or(x.y); - x.w = f64_param(p, "w").unwrap_or(x.w).max(0.0); - x.h = f64_param(p, "h").unwrap_or(x.h).max(0.0); + x.x = clamp_pos(f64_param(p, "x").unwrap_or(x.x)); + x.y = clamp_pos(f64_param(p, "y").unwrap_or(x.y)); + x.w = clamp_side(f64_param(p, "w").unwrap_or(x.w)); + x.h = clamp_side(f64_param(p, "h").unwrap_or(x.h)); } Ok(()) }) diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 9d97c8e6..d9a34030 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3919,3 +3919,37 @@ fn flipping_a_rotated_shape_mirrors_its_outline() { assert_eq!(sorted(corners(&s, id)), sorted(before), "{cmd} twice restores the outline"); } } + +#[test] +fn shape_geometry_commands_limit_size_and_position() { + let mut s = Session::with_new(); + s.execute("slide.new", &json!({"layout": "blank"})).unwrap(); + s.execute("shape.insert", &json!({"preset": "moon", "rect": [100, 100, 200, 100]})).unwrap(); + let xfrm = |s: &Session| s.doc().unwrap().selected_shapes()[0].xfrm.unwrap(); + let within = |s: &Session| { + let x = xfrm(s); + [x.w, x.h].iter().all(|v| v.is_finite() && (0.0..=15_840.0).contains(v)) && [x.x, x.y].iter().all(|v| v.is_finite() && v.abs() <= 100_000.0) + }; + s.execute("shape.setBounds", &json!({"x": 1e100, "y": -1e100, "w": 100000, "h": 1e100})).unwrap(); + let x = xfrm(&s); + assert!(within(&s), "setBounds stored {x:?}"); + s.execute("shape.resize", &json!({"w": 10, "h": 10})).unwrap(); + s.execute("shape.resize", &json!({"h": 1e100})).unwrap(); + let x = xfrm(&s); + assert!(within(&s), "resize stored {x:?}"); + s.execute("shape.resize", &json!({"h": 1e100, "lockAspect": true})).unwrap(); + let x = xfrm(&s); + assert!(within(&s), "locked resize stored {x:?}"); + s.execute("shape.move", &json!({"x": 1e100, "y": -1e100})).unwrap(); + let x = xfrm(&s); + assert!(within(&s), "move stored {x:?}"); + // A move that overflows to infinity lands on the limit, not back at 0. + s.execute("shape.move", &json!({"x": 1e308, "dx": 1e308})).unwrap(); + assert_eq!(xfrm(&s).x, 100_000.0); + // A rotated shape resizes about its centre and still stays inside the limit. + s.execute("shape.setBounds", &json!({"x": 99_000, "y": 0, "w": 10, "h": 10})).unwrap(); + s.execute("shape.rotate", &json!({"deg": 45})).unwrap(); + s.execute("shape.resize", &json!({"w": 1e100})).unwrap(); + let x = xfrm(&s); + assert!(within(&s), "rotated resize stored {x:?}"); +}