diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index dfa6474..d62d4f0 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -1151,6 +1151,28 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result { ) .into()); } + // The width of that window, which the check above cannot see: + // it asks whether the window is *open*, never how wide it is, + // and a `sub` line is the one number in this format a hand (or + // a hostile actor) can set to `i64::MAX` and have accepted. + // Every cut, split, ripple and group trim re-derives the two + // halves of the window by proportion + // ([`crate::project::SubClip::window_at`]), a multiply that + // overflowed on the first frame inside the caption -- a panic + // on the UI thread the first time a hand ripple-deleted across + // it, and in a release build a silently wrapped clock. The + // same bound [`crate::is_resolution`] holds a `resolution` + // line to, and for the same reason: refused by name at the + // door rather than clamped into something nobody wrote. + if sub.out_us - sub.in_us > crate::project::MAX_WINDOW_US { + return Err(format!( + "line {n}: caption at {} is a {}us window, past the {}us a caption can hold", + sub.start, + sub.out_us - sub.in_us, + crate::project::MAX_WINDOW_US + ) + .into()); + } // The palette bound, which is a clip's source bound for words: // the `subtitle` lines are written before these and a caption // names one of them by position. diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index cdeb0f9..fe5ec2a 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -92,6 +92,21 @@ pub const MIN_GAIN_DB: f32 = -60.0; /// a mix bus can take before the limiter is doing all the work. pub const MAX_GAIN_DB: f32 = 12.0; +/// The longest `[in_us, out_us)` window a [`SubClip`] may show, in +/// microseconds: a day of track, and past it the number is not a caption any +/// project holds -- a `.edith` a hand edited (or a hostile actor wrote) reaches +/// this with a line the never-empty check at the parser cannot see past, +/// because `out_us > in_us` says the window is *open* and never how wide it is. +/// +/// A ceiling rather than a clamp for the reason [`crate::is_resolution`] is +/// one: a window this wide is a number nobody meant, and quietly turning it +/// into a day-long caption would put words on screen for a day. The loader +/// refuses it by name instead, next to every other range check a `.edith` line +/// has to pass -- and the arithmetic that cuts a window ([`SubClip::window_at`]) +/// saturates besides, so no caller can reintroduce the wrap this door is here +/// to stop. +pub const MAX_WINDOW_US: i64 = 86_400_000_000; + /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for /// [`Clip`]'s sake -- a clip is `Copy` *and* `Eq`, a float is neither exactly @@ -636,9 +651,10 @@ impl SubClip { } /// How much of its track it shows, in microseconds; `>= 1` by the - /// never-empty invariant. + /// never-empty invariant, and never more than [`MAX_WINDOW_US`] for one + /// that came through a door. pub fn window_us(&self) -> i64 { - self.out_us - self.in_us + self.out_us.saturating_sub(self.in_us) } /// Where in its window the timeline frame `frame` falls, in microseconds -- @@ -649,9 +665,19 @@ impl SubClip { /// before `frame` is the fraction of the microseconds before it. That is /// what lets a subtitle be cut by [`clear`]'s and [`open_room`]'s twins /// without either of them learning an fps. + /// + /// Total arithmetic, and clamped to the window it is cutting: the + /// `saturating_mul` is what a `SubClip` built by a caller rather than a + /// door gets (a hand-built one, a unit test, a future door that forgot the + /// bound) -- a wrapped window would put a caption's clock *before* its own + /// start, and the split that produced it would then hand back two halves + /// whose windows do not meet. The clamp is the never-empty invariant + /// restated: the answer is a point inside `[in_us, out_us]` or nothing. fn window_at(&self, frame: u32) -> i64 { let into = i64::from(frame.saturating_sub(self.start)); - self.in_us + self.window_us() * into / i64::from(self.frames.max(1)) + let window = self.window_us().max(0); + let at = window.saturating_mul(into) / i64::from(self.frames.max(1)); + self.in_us.saturating_add(at.min(window)) } } @@ -1699,6 +1725,20 @@ impl Project { ) .into()); } + // The width of that window, which the check above cannot see: + // [`edith`](crate::edith) asks the same question at the parse + // and this repeats it, because `with_subs` is the door a + // hand-built `Project` comes through and a caller is not more + // trustworthy than a file. + if s.out_us - s.in_us > MAX_WINDOW_US { + return Err(format!( + "{name} caption at {} is a {}us window, past the {}us a caption can hold", + s.start, + s.out_us - s.in_us, + MAX_WINDOW_US + ) + .into()); + } if s.track >= palette { return Err(format!( "{name} caption at {} names subtitle track {} of {palette}", @@ -1965,6 +2005,19 @@ impl Project { ) .into()); } + // The width of that window, which the check above cannot see: the same + // bound the two file doors hold a placement to + // ([`edith`](crate::edith)'s parse and [`with_subs`]), asked here + // because this is the door a *dragged* caption comes through and the + // hand that dragged it builds the `SubClip` itself. + if sub.out_us - sub.in_us > MAX_WINDOW_US { + return Err(format!( + "that placement is a {}us window, past the {}us a caption can hold", + sub.out_us - sub.in_us, + MAX_WINDOW_US + ) + .into()); + } if sub.track >= self.subtitles.len() { return Err(format!( "it names subtitle track {} of {}", @@ -2191,15 +2244,23 @@ impl Project { Edge::Start => { // Clamped to a window that is still a window: the walls above // are frames and the rounding between the two clocks is not - // exact, and neither is a caller's hand-built placement. - s.in_us = (s.in_us + us_of(i64::from(to) - i64::from(s.start), fps)) + // exact, and neither is a caller's hand-built placement. The + // add saturates for the reason [`SubClip::window_at`]'s does -- + // this is one of the two places a caption's clock is composed, + // and a `SubClip` that did not come through a door is the one + // whose `in_us` may already be at the top of `i64`. + s.in_us = s + .in_us + .saturating_add(us_of(i64::from(to) - i64::from(s.start), fps)) .clamp(0, s.out_us - 1); s.frames = s.end() - to; s.start = to; } Edge::End => { - s.out_us = - (s.out_us + us_of(i64::from(to) - i64::from(s.end()), fps)).max(s.in_us + 1); + s.out_us = s + .out_us + .saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps)) + .max(s.in_us + 1); s.frames = to - s.start; } } @@ -5682,15 +5743,21 @@ fn sub_edge_room(subs: &[SubClip], idx: usize, edge: Edge) -> (u32, u32) { /// the window it already shows over the frames it already takes. fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) { let per_frame = s.window_us() / i64::from(s.frames.max(1)); + // The `saturating_add` beside the `saturating_mul` is the same claim + // [`SubClip::window_at`]'s arithmetic makes, in the other direction: this + // one *grows* a caption's clock, and a `SubClip` that reached here without + // a door behind it may already be at the top of `i64`, where a plain add + // wrapped the window negative and the two halves a trim hands back no + // longer met. let by = |from: u32, to: u32| per_frame.saturating_mul(i64::from(to) - i64::from(from)); match edge { Edge::Start => { - s.in_us = (s.in_us + by(s.start, to)).clamp(0, s.out_us - 1); + s.in_us = s.in_us.saturating_add(by(s.start, to)).clamp(0, s.out_us - 1); s.frames = s.end() - to; s.start = to; } Edge::End => { - s.out_us = (s.out_us + by(s.end(), to)).max(s.in_us + 1); + s.out_us = s.out_us.saturating_add(by(s.end(), to)).max(s.in_us + 1); s.frames = to - s.start; } } diff --git a/crates/engine/tests/saveload.rs b/crates/engine/tests/saveload.rs index 68993f3..3b77c6c 100644 --- a/crates/engine/tests/saveload.rs +++ b/crates/engine/tests/saveload.rs @@ -959,3 +959,127 @@ fn a_song_only_project_comes_back_at_the_rate_it_names() { let loaded = PlaybackSession::open_project(&old).expect("a v8 song project opens"); assert_eq!(loaded.meta().frame_rate, 30.0, "the canvas rate, as before"); } + +/// A hand-edited `.edith` whose caption names a microsecond window no project +/// can hold: the whole load, the same edit a 30-second window survives, and the +/// refusal the loader owes for it. +/// +/// A caption's window is the one unbounded number this format takes. The +/// never-empty check at `edith.rs` asks whether `out_us > in_us` and never how +/// large either is, so `out_us = i64::MAX` was a legal line -- and every split, +/// ripple and group trim since cuts the window *by proportion* +/// (`SubClip::window_at`), a plain `window_us() * into` that overflows on the +/// first frame inside the caption. That is a panic on the UI thread the first +/// time a hand ripple-deletes across the caption, and in a release build the +/// same line wraps the caption's clock to a negative number instead. So the +/// window is bounded at the door, by name, and the arithmetic behind the cut +/// is made total as well. +#[test] +fn a_caption_window_no_project_can_hold_is_refused_by_name() { + let dir = scratch("caption_window"); + let media = copy_in(&dir, "test_av.mp4"); + let subs = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/data/test_subs.srt") + .canonicalize() + .expect("the subtitle fixture"); + + // The control first: a 30-second window over 30 frames, the shape every + // save writes, and the shape this file's whole edit path is proved on. + let ok = dir.join("window_ok.edith"); + std::fs::write( + &ok, + format!( + "edith 23\nplayhead 0\nresolution 1280 720\nsource 0 {}\nsubtitle - {}\n\ + video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\ + sub 1 0 30 0 0 30000000\n", + media.display(), + subs.display(), + ), + ) + .expect("write"); + let mut control = PlaybackSession::open_project(&ok).expect("a 30s caption window opens"); + assert_eq!(control.sub_lane(Lane::S1)[0].out_us, 30_000_000); + // The edits the unbounded window used to survive: a split inside the + // caption and a ripple across it. Both cut the window by proportion. + assert!(control.cut_at(0.5), "the playhead splits the caption under it"); + assert_eq!( + control.sub_lane(Lane::S1).len(), + 2, + "the split cut the caption in two" + ); + control.undo(); + control + .cut_regions(&[(0, 10)], &[Lane::S1]) + .expect("a ripple across the caption, scoped to its own lane"); + let after = control.sub_lane(Lane::S1); + assert_eq!(after.len(), 1, "the caption's tail is still there"); + assert_eq!( + (after[0].start, after[0].frames), + (0, 20), + "and it closed the hole the cut left" + ); + + // The same file, the same lane, the same edit -- with a window no project + // can hold. This is the line that used to load and then panic. + let huge = dir.join("window_huge.edith"); + std::fs::write( + &huge, + format!( + "edith 23\nplayhead 0\nresolution 1280 720\nsource 0 {}\nsubtitle - {}\n\ + video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\ + sub 1 0 30 0 0 9223372036854775807\n", + media.display(), + subs.display(), + ), + ) + .expect("write"); + let err = PlaybackSession::open_project(&huge) + .err() + .unwrap_or_else(|| { + panic!( + "a caption window of {}us loaded, and the first split across it \ + panics in SubClip::window_at", + i64::MAX + ) + }) + .to_string(); + assert!( + err.contains("window") && err.contains("9223372036854775807"), + "the refusal names the window it is refusing, and the value: {err}" + ); + assert!(err.starts_with("line 7"), "and which line said it: {err}"); + + // The same bound at the other two doors a caption comes through, because a + // caller is not more trustworthy than a file: the placement a hand drags + // out of the palette is a `SubClip` somebody built, and it goes through + // `place_sub` rather than the parse. + let mut session = PlaybackSession::open(&media).expect("open the fixture"); + session + .import_subtitles(&subs) + .expect("the fixture's one track"); + let s1 = session.add_lane(engine::project::LaneKind::Subtitle); + let err = session + .place_sub( + s1, + 0, + engine::project::SubClip { + start: 0, + frames: 30, + track: 0, + in_us: 0, + out_us: i64::MAX, + link: None, + }, + ) + .expect_err("a window no project can hold is refused by name, not clamped") + .to_string(); + assert!( + err.contains("window") && err.contains(&i64::MAX.to_string()), + "the placement refusal names the window and the value: {err}" + ); + assert!( + session.sub_lane(s1).is_empty(), + "and a refusal places nothing: {:?}", + session.sub_lane(s1) + ); +}