forge
Explore
Log in

Don't you dare go Hollow

Bound a caption's microsecond window at the door

Browse files at this commit

70ba4ebbef2a253ba0fd96f352e4e54b09aa5c0fAhmet Kaan GümüşParent: 0c9d76c
  • fix
    • bound a caption's microsecond window at the door
Show full message
fix(engine): bound a caption's microsecond window at the door

A `sub` line's `in_us`/`out_us` is the one number in the `.edith`
format with no magnitude bound: the parse asks whether the window is
*open* (`out_us > in_us`) and never how wide it is, and `micros()` is a
bare i64 parse. So `out_us = 9223372036854775807` was a legal line.

Every split, ripple and group trim re-derives the two halves of the
window by proportion -- `SubClip::window_at`, a plain
`window_us() * into` -- which overflows on the first frame inside the
caption. A hand-edited or hostile `.edith` therefore panics the editor
on the UI thread the first time a user ripple-deletes across, splits or
trims that caption, losing unsaved work, and the file re-crashes on the
next open. In a release build the same line wraps the caption's clock
negative instead: `trim_sub`'s only check on its result is a
`debug_assert!`.

Bounded at the door, by name, beside every other range check a `.edith`
line already has to pass, and the composing arithmetic made total so a
future caller cannot reintroduce the wrap:

- `project::MAX_WINDOW_US` -- a day of track, the same shape of refusal
  as `is_resolution` for a `resolution` line.
- checked at all three doors a caption comes through: the parse
  (`edith.rs`), `Project::with_subs` (a hand-built `Project`) and
  `Project::place_sub` (a dragged placement).
- `SubClip::window_at` saturates its multiply and clamps its answer into
  the window it is cutting; `SubClip::window_us` saturates its
  subtraction; `trim_sub` and `write_sub_edge` saturate their additions.

Witness (`crates/engine/tests/saveload.rs`, hand-built project file, no
`--release`): the 30-second-window control loads, splits and ripples
across; the same file with `out_us = i64::MAX` used to load and then
overflow at `project.rs:654` on both `cut_at` and `cut_regions`, and is
now refused by name at `line 7`. Non-vacuity: with the parse bound
disabled the test goes red on `with_subs`' refusal instead, and with
both disabled it loads again.
crates/engine/src/edith.rsmodified+22 -0
@@ -1151,6 +1151,28 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
1151 )1151 )
1152 .into());1152 .into());
1153 }1153 }
1154+ // The width of that window, which the check above cannot see:
1155+ // it asks whether the window is *open*, never how wide it is,
1156+ // and a `sub` line is the one number in this format a hand (or
1157+ // a hostile actor) can set to `i64::MAX` and have accepted.
1158+ // Every cut, split, ripple and group trim re-derives the two
1159+ // halves of the window by proportion
1160+ // ([`crate::project::SubClip::window_at`]), a multiply that
1161+ // overflowed on the first frame inside the caption -- a panic
1162+ // on the UI thread the first time a hand ripple-deleted across
1163+ // it, and in a release build a silently wrapped clock. The
1164+ // same bound [`crate::is_resolution`] holds a `resolution`
1165+ // line to, and for the same reason: refused by name at the
1166+ // door rather than clamped into something nobody wrote.
1167+ if sub.out_us - sub.in_us > crate::project::MAX_WINDOW_US {
1168+ return Err(format!(
1169+ "line {n}: caption at {} is a {}us window, past the {}us a caption can hold",
1170+ sub.start,
1171+ sub.out_us - sub.in_us,
1172+ crate::project::MAX_WINDOW_US
1173+ )
1174+ .into());
1175+ }
1154 // The palette bound, which is a clip's source bound for words:1176 // The palette bound, which is a clip's source bound for words:
1155 // the `subtitle` lines are written before these and a caption1177 // the `subtitle` lines are written before these and a caption
1156 // names one of them by position.1178 // names one of them by position.
crates/engine/src/project.rsmodified+76 -9
@@ -92,6 +92,21 @@ pub const MIN_GAIN_DB: f32 = -60.0;
92 /// a mix bus can take before the limiter is doing all the work.92 /// a mix bus can take before the limiter is doing all the work.
93 pub const MAX_GAIN_DB: f32 = 12.0;93 pub const MAX_GAIN_DB: f32 = 12.0;
94 94
95+/// The longest `[in_us, out_us)` window a [`SubClip`] may show, in
96+/// microseconds: a day of track, and past it the number is not a caption any
97+/// project holds -- a `.edith` a hand edited (or a hostile actor wrote) reaches
98+/// this with a line the never-empty check at the parser cannot see past,
99+/// because `out_us > in_us` says the window is *open* and never how wide it is.
100+///
101+/// A ceiling rather than a clamp for the reason [`crate::is_resolution`] is
102+/// one: a window this wide is a number nobody meant, and quietly turning it
103+/// into a day-long caption would put words on screen for a day. The loader
104+/// refuses it by name instead, next to every other range check a `.edith` line
105+/// has to pass -- and the arithmetic that cuts a window ([`SubClip::window_at`])
106+/// saturates besides, so no caller can reintroduce the wrap this door is here
107+/// to stop.
108+pub const MAX_WINDOW_US: i64 = 86_400_000_000;
109+
95 /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it110 /// How fast a clip plays, in **thousandths of real time**: 1000 is the speed it
96 /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for111 /// was shot at, 2000 twice that, 500 half. An integer and not an `f32` for
97 /// [`Clip`]'s sake -- a clip is `Copy` *and* `Eq`, a float is neither exactly112 /// [`Clip`]'s sake -- a clip is `Copy` *and* `Eq`, a float is neither exactly
@@ -636,9 +651,10 @@ impl SubClip {
636 }651 }
637 652
638 /// How much of its track it shows, in microseconds; `>= 1` by the653 /// How much of its track it shows, in microseconds; `>= 1` by the
639- /// never-empty invariant.654+ /// never-empty invariant, and never more than [`MAX_WINDOW_US`] for one
655+ /// that came through a door.
640 pub fn window_us(&self) -> i64 {656 pub fn window_us(&self) -> i64 {
641- self.out_us - self.in_us657+ self.out_us.saturating_sub(self.in_us)
642 }658 }
643 659
644 /// Where in its window the timeline frame `frame` falls, in microseconds --660 /// Where in its window the timeline frame `frame` falls, in microseconds --
@@ -649,9 +665,19 @@ impl SubClip {
649 /// before `frame` is the fraction of the microseconds before it. That is665 /// before `frame` is the fraction of the microseconds before it. That is
650 /// what lets a subtitle be cut by [`clear`]'s and [`open_room`]'s twins666 /// what lets a subtitle be cut by [`clear`]'s and [`open_room`]'s twins
651 /// without either of them learning an fps.667 /// without either of them learning an fps.
668+ ///
669+ /// Total arithmetic, and clamped to the window it is cutting: the
670+ /// `saturating_mul` is what a `SubClip` built by a caller rather than a
671+ /// door gets (a hand-built one, a unit test, a future door that forgot the
672+ /// bound) -- a wrapped window would put a caption's clock *before* its own
673+ /// start, and the split that produced it would then hand back two halves
674+ /// whose windows do not meet. The clamp is the never-empty invariant
675+ /// restated: the answer is a point inside `[in_us, out_us]` or nothing.
652 fn window_at(&self, frame: u32) -> i64 {676 fn window_at(&self, frame: u32) -> i64 {
653 let into = i64::from(frame.saturating_sub(self.start));677 let into = i64::from(frame.saturating_sub(self.start));
654- self.in_us + self.window_us() * into / i64::from(self.frames.max(1))678+ let window = self.window_us().max(0);
679+ let at = window.saturating_mul(into) / i64::from(self.frames.max(1));
680+ self.in_us.saturating_add(at.min(window))
655 }681 }
656 }682 }
657 683
@@ -1699,6 +1725,20 @@ impl Project {
1699 )1725 )
1700 .into());1726 .into());
1701 }1727 }
1728+ // The width of that window, which the check above cannot see:
1729+ // [`edith`](crate::edith) asks the same question at the parse
1730+ // and this repeats it, because `with_subs` is the door a
1731+ // hand-built `Project` comes through and a caller is not more
1732+ // trustworthy than a file.
1733+ if s.out_us - s.in_us > MAX_WINDOW_US {
1734+ return Err(format!(
1735+ "{name} caption at {} is a {}us window, past the {}us a caption can hold",
1736+ s.start,
1737+ s.out_us - s.in_us,
1738+ MAX_WINDOW_US
1739+ )
1740+ .into());
1741+ }
1702 if s.track >= palette {1742 if s.track >= palette {
1703 return Err(format!(1743 return Err(format!(
1704 "{name} caption at {} names subtitle track {} of {palette}",1744 "{name} caption at {} names subtitle track {} of {palette}",
@@ -1965,6 +2005,19 @@ impl Project {
1965 )2005 )
1966 .into());2006 .into());
1967 }2007 }
2008+ // The width of that window, which the check above cannot see: the same
2009+ // bound the two file doors hold a placement to
2010+ // ([`edith`](crate::edith)'s parse and [`with_subs`]), asked here
2011+ // because this is the door a *dragged* caption comes through and the
2012+ // hand that dragged it builds the `SubClip` itself.
2013+ if sub.out_us - sub.in_us > MAX_WINDOW_US {
2014+ return Err(format!(
2015+ "that placement is a {}us window, past the {}us a caption can hold",
2016+ sub.out_us - sub.in_us,
2017+ MAX_WINDOW_US
2018+ )
2019+ .into());
2020+ }
1968 if sub.track >= self.subtitles.len() {2021 if sub.track >= self.subtitles.len() {
1969 return Err(format!(2022 return Err(format!(
1970 "it names subtitle track {} of {}",2023 "it names subtitle track {} of {}",
@@ -2191,15 +2244,23 @@ impl Project {
2191 Edge::Start => {2244 Edge::Start => {
2192 // Clamped to a window that is still a window: the walls above2245 // Clamped to a window that is still a window: the walls above
2193 // are frames and the rounding between the two clocks is not2246 // are frames and the rounding between the two clocks is not
2194- // exact, and neither is a caller's hand-built placement.2247+ // exact, and neither is a caller's hand-built placement. The
2195- s.in_us = (s.in_us + us_of(i64::from(to) - i64::from(s.start), fps))2248+ // add saturates for the reason [`SubClip::window_at`]'s does --
2249+ // this is one of the two places a caption's clock is composed,
2250+ // and a `SubClip` that did not come through a door is the one
2251+ // whose `in_us` may already be at the top of `i64`.
2252+ s.in_us = s
2253+ .in_us
2254+ .saturating_add(us_of(i64::from(to) - i64::from(s.start), fps))
2196 .clamp(0, s.out_us - 1);2255 .clamp(0, s.out_us - 1);
2197 s.frames = s.end() - to;2256 s.frames = s.end() - to;
2198 s.start = to;2257 s.start = to;
2199 }2258 }
2200 Edge::End => {2259 Edge::End => {
2201- s.out_us =2260+ s.out_us = s
2202- (s.out_us + us_of(i64::from(to) - i64::from(s.end()), fps)).max(s.in_us + 1);2261+ .out_us
2262+ .saturating_add(us_of(i64::from(to) - i64::from(s.end()), fps))
2263+ .max(s.in_us + 1);
2203 s.frames = to - s.start;2264 s.frames = to - s.start;
2204 }2265 }
2205 }2266 }
@@ -5682,15 +5743,21 @@ fn sub_edge_room(subs: &[SubClip], idx: usize, edge: Edge) -> (u32, u32) {
5682 /// the window it already shows over the frames it already takes.5743 /// the window it already shows over the frames it already takes.
5683 fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) {5744 fn write_sub_edge(s: &mut SubClip, edge: Edge, to: u32) {
5684 let per_frame = s.window_us() / i64::from(s.frames.max(1));5745 let per_frame = s.window_us() / i64::from(s.frames.max(1));
5746+ // The `saturating_add` beside the `saturating_mul` is the same claim
5747+ // [`SubClip::window_at`]'s arithmetic makes, in the other direction: this
5748+ // one *grows* a caption's clock, and a `SubClip` that reached here without
5749+ // a door behind it may already be at the top of `i64`, where a plain add
5750+ // wrapped the window negative and the two halves a trim hands back no
5751+ // longer met.
5685 let by = |from: u32, to: u32| per_frame.saturating_mul(i64::from(to) - i64::from(from));5752 let by = |from: u32, to: u32| per_frame.saturating_mul(i64::from(to) - i64::from(from));
5686 match edge {5753 match edge {
5687 Edge::Start => {5754 Edge::Start => {
5688- s.in_us = (s.in_us + by(s.start, to)).clamp(0, s.out_us - 1);5755+ s.in_us = s.in_us.saturating_add(by(s.start, to)).clamp(0, s.out_us - 1);
5689 s.frames = s.end() - to;5756 s.frames = s.end() - to;
5690 s.start = to;5757 s.start = to;
5691 }5758 }
5692 Edge::End => {5759 Edge::End => {
5693- s.out_us = (s.out_us + by(s.end(), to)).max(s.in_us + 1);5760+ s.out_us = s.out_us.saturating_add(by(s.end(), to)).max(s.in_us + 1);
5694 s.frames = to - s.start;5761 s.frames = to - s.start;
5695 }5762 }
5696 }5763 }
crates/engine/tests/saveload.rsmodified+124 -0
@@ -959,3 +959,127 @@ fn a_song_only_project_comes_back_at_the_rate_it_names() {
959 let loaded = PlaybackSession::open_project(&old).expect("a v8 song project opens");959 let loaded = PlaybackSession::open_project(&old).expect("a v8 song project opens");
960 assert_eq!(loaded.meta().frame_rate, 30.0, "the canvas rate, as before");960 assert_eq!(loaded.meta().frame_rate, 30.0, "the canvas rate, as before");
961 }961 }
962+
963+/// A hand-edited `.edith` whose caption names a microsecond window no project
964+/// can hold: the whole load, the same edit a 30-second window survives, and the
965+/// refusal the loader owes for it.
966+///
967+/// A caption's window is the one unbounded number this format takes. The
968+/// never-empty check at `edith.rs` asks whether `out_us > in_us` and never how
969+/// large either is, so `out_us = i64::MAX` was a legal line -- and every split,
970+/// ripple and group trim since cuts the window *by proportion*
971+/// (`SubClip::window_at`), a plain `window_us() * into` that overflows on the
972+/// first frame inside the caption. That is a panic on the UI thread the first
973+/// time a hand ripple-deletes across the caption, and in a release build the
974+/// same line wraps the caption's clock to a negative number instead. So the
975+/// window is bounded at the door, by name, and the arithmetic behind the cut
976+/// is made total as well.
977+#[test]
978+fn a_caption_window_no_project_can_hold_is_refused_by_name() {
979+ let dir = scratch("caption_window");
980+ let media = copy_in(&dir, "test_av.mp4");
981+ let subs = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
982+ .join("tests/data/test_subs.srt")
983+ .canonicalize()
984+ .expect("the subtitle fixture");
985+
986+ // The control first: a 30-second window over 30 frames, the shape every
987+ // save writes, and the shape this file's whole edit path is proved on.
988+ let ok = dir.join("window_ok.edith");
989+ std::fs::write(
990+ &ok,
991+ format!(
992+ "edith 23\nplayhead 0\nresolution 1280 720\nsource 0 {}\nsubtitle - {}\n\
993+ video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
994+ sub 1 0 30 0 0 30000000\n",
995+ media.display(),
996+ subs.display(),
997+ ),
998+ )
999+ .expect("write");
1000+ let mut control = PlaybackSession::open_project(&ok).expect("a 30s caption window opens");
1001+ assert_eq!(control.sub_lane(Lane::S1)[0].out_us, 30_000_000);
1002+ // The edits the unbounded window used to survive: a split inside the
1003+ // caption and a ripple across it. Both cut the window by proportion.
1004+ assert!(control.cut_at(0.5), "the playhead splits the caption under it");
1005+ assert_eq!(
1006+ control.sub_lane(Lane::S1).len(),
1007+ 2,
1008+ "the split cut the caption in two"
1009+ );
1010+ control.undo();
1011+ control
1012+ .cut_regions(&[(0, 10)], &[Lane::S1])
1013+ .expect("a ripple across the caption, scoped to its own lane");
1014+ let after = control.sub_lane(Lane::S1);
1015+ assert_eq!(after.len(), 1, "the caption's tail is still there");
1016+ assert_eq!(
1017+ (after[0].start, after[0].frames),
1018+ (0, 20),
1019+ "and it closed the hole the cut left"
1020+ );
1021+
1022+ // The same file, the same lane, the same edit -- with a window no project
1023+ // can hold. This is the line that used to load and then panic.
1024+ let huge = dir.join("window_huge.edith");
1025+ std::fs::write(
1026+ &huge,
1027+ format!(
1028+ "edith 23\nplayhead 0\nresolution 1280 720\nsource 0 {}\nsubtitle - {}\n\
1029+ video 1 0 0 30 0 - - - fit 1000 0 0 0 - 0 0\n\
1030+ sub 1 0 30 0 0 9223372036854775807\n",
1031+ media.display(),
1032+ subs.display(),
1033+ ),
1034+ )
1035+ .expect("write");
1036+ let err = PlaybackSession::open_project(&huge)
1037+ .err()
1038+ .unwrap_or_else(|| {
1039+ panic!(
1040+ "a caption window of {}us loaded, and the first split across it \
1041+ panics in SubClip::window_at",
1042+ i64::MAX
1043+ )
1044+ })
1045+ .to_string();
1046+ assert!(
1047+ err.contains("window") && err.contains("9223372036854775807"),
1048+ "the refusal names the window it is refusing, and the value: {err}"
1049+ );
1050+ assert!(err.starts_with("line 7"), "and which line said it: {err}");
1051+
1052+ // The same bound at the other two doors a caption comes through, because a
1053+ // caller is not more trustworthy than a file: the placement a hand drags
1054+ // out of the palette is a `SubClip` somebody built, and it goes through
1055+ // `place_sub` rather than the parse.
1056+ let mut session = PlaybackSession::open(&media).expect("open the fixture");
1057+ session
1058+ .import_subtitles(&subs)
1059+ .expect("the fixture's one track");
1060+ let s1 = session.add_lane(engine::project::LaneKind::Subtitle);
1061+ let err = session
1062+ .place_sub(
1063+ s1,
1064+ 0,
1065+ engine::project::SubClip {
1066+ start: 0,
1067+ frames: 30,
1068+ track: 0,
1069+ in_us: 0,
1070+ out_us: i64::MAX,
1071+ link: None,
1072+ },
1073+ )
1074+ .expect_err("a window no project can hold is refused by name, not clamped")
1075+ .to_string();
1076+ assert!(
1077+ err.contains("window") && err.contains(&i64::MAX.to_string()),
1078+ "the placement refusal names the window and the value: {err}"
1079+ );
1080+ assert!(
1081+ session.sub_lane(s1).is_empty(),
1082+ "and a refusal places nothing: {:?}",
1083+ session.sub_lane(s1)
1084+ );
1085+}