Bound a clip's transform position, and sweep the loader class
Browse files at this commitd964f6ced095445e8423780d3fe602ffadf8fb74Parent: 1b0fbe8- fix
- bound a clip's transform position, and sweep the loader class
1 changed file +48 -2
- crates/engine/src/scale.rs +48 -2
crates/engine/src/scale.rsmodified+48 -2
| @@ -390,10 +390,21 @@ pub fn transformed_dst_rect( | ||
| 390 | 390 | let cap_h = i64::from(canvas_h.max(1)) * 1024; |
| 391 | 391 | let new_w = ((f64::from(dst.w) * scale).round() as i64).clamp(1, cap_w); |
| 392 | 392 | let new_h = ((f64::from(dst.h) * scale).round() as i64).clamp(1, cap_h); |
| 393 | + // ...and the position, to the same ceiling, for the same reason. A | |
| 394 | + // `transform` line takes any finite `f32` for `pos_x`/`pos_y` (there is | |
| 395 | + // no per-field clamp at the parse, unlike `scale`'s use of the value), and | |
| 396 | + // `f64 as i64` *saturates* -- so a hostile 3e38 arrived here as `i64::MAX` | |
| 397 | + // and the additions below, not the cast, are what overflowed: a panic on | |
| 398 | + // the compositing thread in a debug build and a silently wrapped position | |
| 399 | + // in a release one. A cap in canvas fractions is the same 1024 the scale | |
| 400 | + // gets, and a box pushed that far off the canvas is long gone either way: | |
| 401 | + // it comes back as the two empty rects the function already answers with. | |
| 393 | 402 | let pos_x = if t.pos_x.is_finite() { f64::from(t.pos_x) } else { 0.0 }; |
| 394 | 403 | let pos_y = if t.pos_y.is_finite() { f64::from(t.pos_y) } else { 0.0 }; |
| 395 | - let cx = i64::from(dst.x) + i64::from(dst.w) / 2 + (f64::from(canvas_w) * pos_x).round() as i64; | |
| 396 | - let cy = i64::from(dst.y) + i64::from(dst.h) / 2 + (f64::from(canvas_h) * pos_y).round() as i64; | |
| 404 | + let cx = (i64::from(dst.x) + i64::from(dst.w) / 2) | |
| 405 | + .saturating_add((f64::from(canvas_w) * pos_x.clamp(-1024.0, 1024.0)).round() as i64); | |
| 406 | + let cy = (i64::from(dst.y) + i64::from(dst.h) / 2) | |
| 407 | + .saturating_add((f64::from(canvas_h) * pos_y.clamp(-1024.0, 1024.0)).round() as i64); | |
| 397 | 408 | // The full box the transform describes, unclamped -- it may start |
| 398 | 409 | // negative or run past either edge, which is exactly what "moved off the |
| 399 | 410 | // canvas" means. |
| @@ -1544,6 +1555,41 @@ mod tests { | ||
| 1544 | 1555 | assert!(d.is_empty() && s.is_empty(), "wholly off-canvas has nothing to draw"); |
| 1545 | 1556 | } |
| 1546 | 1557 | |
| 1558 | + /// `pos_x`/`pos_y` are canvas fractions and a `transform` line takes any | |
| 1559 | + /// finite `f32`, so they are the one transform field with no bound on the | |
| 1560 | + /// way in -- `float()` (edith.rs) refuses only inf/NaN, and unlike | |
| 1561 | + /// `scale`, which this function caps at a thousand canvas widths precisely | |
| 1562 | + /// so its own arithmetic cannot overflow, the position was multiplied by | |
| 1563 | + /// the canvas and added to `dst.x` unchecked. Rust's `as i64` saturates, so | |
| 1564 | + /// a hostile 3e38 became `i64::MAX` and the *add* is what overflowed: | |
| 1565 | + /// a panic on the compositing thread in a debug build, a silently wrapped | |
| 1566 | + /// position in a release one. A placement pushed off the canvas is already | |
| 1567 | + /// a legal answer (the two empty rects above); a position past the point | |
| 1568 | + /// where the picture is long gone must give that same answer. | |
| 1569 | + #[test] | |
| 1570 | + fn a_transform_position_a_file_can_name_does_not_overflow_the_centre() { | |
| 1571 | + let dst = Rect { x: 10, y: 20, w: 100, h: 100 }; | |
| 1572 | + let src = Rect { x: 0, y: 0, w: 100, h: 100 }; | |
| 1573 | + for (pos_x, pos_y) in [ | |
| 1574 | + (3.0e38f32, 0.0), | |
| 1575 | + (-3.0e38, 0.0), | |
| 1576 | + (0.0, 3.0e38), | |
| 1577 | + (0.0, -3.0e38), | |
| 1578 | + (3.0e38, 3.0e38), | |
| 1579 | + (f32::MAX, f32::MIN), | |
| 1580 | + ] { | |
| 1581 | + let t = TransformParams { pos_x, pos_y, ..Default::default() }; | |
| 1582 | + let (d, s) = transformed_dst_rect(dst, src, 100, 100, &t, 200, 200); | |
| 1583 | + // The answer is "nothing to draw" -- the box is a thousand canvas | |
| 1584 | + // widths off screen -- not an overflow and not a wrapped position | |
| 1585 | + // that lands back on the canvas. | |
| 1586 | + assert!( | |
| 1587 | + d.is_empty() && s.is_empty(), | |
| 1588 | + "pos ({pos_x}, {pos_y}) gave {d:?} / {s:?} rather than an empty rect pair" | |
| 1589 | + ); | |
| 1590 | + } | |
| 1591 | + } | |
| 1592 | + | |
| 1547 | 1593 | /// [`nearest_90_steps`] rounds to the closest quarter turn and folds a |
| 1548 | 1594 | /// non-finite value to the identity. |
| 1549 | 1595 | #[test] |