From d964f6ced095445e8423780d3fe602ffadf8fb74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 30 Sep 2026 16:28:40 +0300 Subject: [PATCH] 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.rs | 50 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/scale.rs b/crates/engine/src/scale.rs index cc780bd..4221eaf 100644 --- a/crates/engine/src/scale.rs +++ b/crates/engine/src/scale.rs @@ -390,10 +390,21 @@ pub fn transformed_dst_rect( let cap_h = i64::from(canvas_h.max(1)) * 1024; let new_w = ((f64::from(dst.w) * scale).round() as i64).clamp(1, cap_w); let new_h = ((f64::from(dst.h) * scale).round() as i64).clamp(1, cap_h); + // ...and the position, to the same ceiling, for the same reason. A + // `transform` line takes any finite `f32` for `pos_x`/`pos_y` (there is + // no per-field clamp at the parse, unlike `scale`'s use of the value), and + // `f64 as i64` *saturates* -- so a hostile 3e38 arrived here as `i64::MAX` + // and the additions below, not the cast, are what overflowed: a panic on + // the compositing thread in a debug build and a silently wrapped position + // in a release one. A cap in canvas fractions is the same 1024 the scale + // gets, and a box pushed that far off the canvas is long gone either way: + // it comes back as the two empty rects the function already answers with. let pos_x = if t.pos_x.is_finite() { f64::from(t.pos_x) } else { 0.0 }; let pos_y = if t.pos_y.is_finite() { f64::from(t.pos_y) } else { 0.0 }; - let cx = i64::from(dst.x) + i64::from(dst.w) / 2 + (f64::from(canvas_w) * pos_x).round() as i64; - let cy = i64::from(dst.y) + i64::from(dst.h) / 2 + (f64::from(canvas_h) * pos_y).round() as i64; + let cx = (i64::from(dst.x) + i64::from(dst.w) / 2) + .saturating_add((f64::from(canvas_w) * pos_x.clamp(-1024.0, 1024.0)).round() as i64); + let cy = (i64::from(dst.y) + i64::from(dst.h) / 2) + .saturating_add((f64::from(canvas_h) * pos_y.clamp(-1024.0, 1024.0)).round() as i64); // The full box the transform describes, unclamped -- it may start // negative or run past either edge, which is exactly what "moved off the // canvas" means. @@ -1544,6 +1555,41 @@ mod tests { assert!(d.is_empty() && s.is_empty(), "wholly off-canvas has nothing to draw"); } + /// `pos_x`/`pos_y` are canvas fractions and a `transform` line takes any + /// finite `f32`, so they are the one transform field with no bound on the + /// way in -- `float()` (edith.rs) refuses only inf/NaN, and unlike + /// `scale`, which this function caps at a thousand canvas widths precisely + /// so its own arithmetic cannot overflow, the position was multiplied by + /// the canvas and added to `dst.x` unchecked. Rust's `as i64` saturates, so + /// a hostile 3e38 became `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. A placement pushed off the canvas is already + /// a legal answer (the two empty rects above); a position past the point + /// where the picture is long gone must give that same answer. + #[test] + fn a_transform_position_a_file_can_name_does_not_overflow_the_centre() { + let dst = Rect { x: 10, y: 20, w: 100, h: 100 }; + let src = Rect { x: 0, y: 0, w: 100, h: 100 }; + for (pos_x, pos_y) in [ + (3.0e38f32, 0.0), + (-3.0e38, 0.0), + (0.0, 3.0e38), + (0.0, -3.0e38), + (3.0e38, 3.0e38), + (f32::MAX, f32::MIN), + ] { + let t = TransformParams { pos_x, pos_y, ..Default::default() }; + let (d, s) = transformed_dst_rect(dst, src, 100, 100, &t, 200, 200); + // The answer is "nothing to draw" -- the box is a thousand canvas + // widths off screen -- not an overflow and not a wrapped position + // that lands back on the canvas. + assert!( + d.is_empty() && s.is_empty(), + "pos ({pos_x}, {pos_y}) gave {d:?} / {s:?} rather than an empty rect pair" + ); + } + } + /// [`nearest_90_steps`] rounds to the closest quarter turn and folds a /// non-finite value to the identity. #[test]