forge
Explore
Log in

Don't you dare go Hollow

Bound a clip's transform position, and sweep the loader class

Browse files at this commit

d964f6ced095445e8423780d3fe602ffadf8fb74Ahmet Kaan GümüşParent: 1b0fbe8
  • fix
    • bound a clip's transform position, and sweep the loader class
Show full message
fix(engine): bound a clip's transform position, and sweep the loader class

The third of the class "a number read out of a file a hand can edit,
used in arithmetic without a bound", and the last one the sweep found.

`transformed_dst_rect` caps `scale` at 1024 canvas widths for exactly
this reason -- the comment at the cap says so -- and `pos_x`/`pos_y`
multiply the canvas and are *added* to the placement's centre with no
bound at all. `float()` in the parser refuses only inf/NaN, so
`transform 3e38 0 1 0 0 0 0 0` is a legal line. `f64 as i64` saturates,
so the value arrived as `i64::MAX` and the ADD is what overflowed: a
panic on the compositing thread in a debug build, a silently wrapped
position in a release one. Clamped to the same +/-1024 canvas fractions
`scale` already gets, with saturating adds, so a position past the point
where the picture is long gone gives the two empty rects the function
already answers with.

The sweep, over every numeric field the `.edith` parser reads and every
config file the app reads. Two rules decide a site: a value is safe
either because the parse bounds it by name, or because a consumer clamps
it unconditionally before the arithmetic -- and only the second kind is
silent about a value the file actually stated.

The parser's four helpers (`number`, `number64`, `micros`, `float`) bound
nothing; the bound is always in the branch or in a consumer.

SAFE (bounded at the parse): resolution (`is_resolution`), fps
(0 < f <= 1000), samplerate (1k..384k), limiter.ceiling_db
(`with_ceiling` at the parse itself), clip geometry (`check()`'s
`start + frames`), clip.speed, the three fade/transition counts
(`.min(frames)`), clip.visualizer (`u8::try_from`), every table index
(`table_index` re-checks), sub.start/frames (`checked_add`), sub.track
(must name a palette row the file has), sub.link (saturating counter).

SAFE (clamped by a consumer, wherever the value came from): eq/color/
transform floats, gain.db (`with_mix`), color.* (`Lut::new`),
transform.scale/rotate/crop_* (`cap_w`, `nearest_90_steps`, the crop
clamp), playhead (`landing`), source.audio_stream (no arithmetic; every
consumer `.get()`s).

KNOWN, NOT A FAULT: clip.viz_paint is an unbounded u64, but every
accessor in `decode.rs` is a bit mask, so there is no overflow -- the
glow byte is read unclamped where the writer clamps it to 16, so a
hand-written file can ask for more work than the UI can produce. A cost,
not a wrong answer. Left alone deliberately.

The app's own config: eight files, only two carry a number. The
stance-splits loader (I-2) is safe because `split_size` re-clamps every
seam on every frame and `Player::split_px` is its only reader -- the
loader stores a value the frame loop then bounds. The subtitle size is
range-checked at the parse, and `RangeInclusive::contains` being a
partial comparison is what makes NaN/inf fall back rather than survive.
The keymap reads no number at all; the rest are whole-file tokens
resolving to a bool or an enum.
crates/engine/src/scale.rsmodified+48 -2
@@ -390,10 +390,21 @@ pub fn transformed_dst_rect(
390390 let cap_h = i64::from(canvas_h.max(1)) * 1024;
391391 let new_w = ((f64::from(dst.w) * scale).round() as i64).clamp(1, cap_w);
392392 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.
393402 let pos_x = if t.pos_x.is_finite() { f64::from(t.pos_x) } else { 0.0 };
394403 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);
397408 // The full box the transform describes, unclamped -- it may start
398409 // negative or run past either edge, which is exactly what "moved off the
399410 // canvas" means.
@@ -1544,6 +1555,41 @@ mod tests {
15441555 assert!(d.is_empty() && s.is_empty(), "wholly off-canvas has nothing to draw");
15451556 }
15461557
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+
15471593 /// [`nearest_90_steps`] rounds to the closest quarter turn and folds a
15481594 /// non-finite value to the identity.
15491595 #[test]