Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

edit: speed changes refuse NaN and never overflow the timeline

Brandon Thomas2f49131f6495
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 2598a0fa0..b2befb84c 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -761,7 +761,7 @@ pub fn set_speed(seq: &mut Sequence, clip: ClipId, speed: f64, reverse: bool, ri
/// by different amounts (a split edit), the later material moves by the largest change, so no
/// member is overlapped and the tracks stay in sync. Nothing changes when the edit fails.
pub fn set_speed_group(seq: &mut Sequence, clips: &[ClipId], speed: f64, reverse: bool, ripple: bool, ctx: &mut EditCtx) -> Result<()> {
- if speed <= 0.0 {
+ if !(speed.is_finite() && speed > 0.0) {
return Err(EditError::Other("speed must be positive".into()));
}
let mut work = seq.clone();
@@ -773,7 +773,9 @@ pub fn set_speed_group(seq: &mut Sequence, clips: &[ClipId], speed: f64, reverse
return Err(EditError::Other("clips on one track change speed one at a time".into()));
}
let src_len = it.source_out() - it.source_in;
- let mut new_dur = Tick((src_len.0 as f64 / speed).round() as i64).max(ctx.min_duration);
+ // a tiny speed saturates the cast: keep the clip's end representable
+ let room = Tick(Tick::MAX.0.saturating_sub(it.start.0.max(0)));
+ let mut new_dur = Tick((src_len.0 as f64 / speed).round() as i64).min(room).max(ctx.min_duration);
let old_end = it.end();
let tr = work.track(tid).ok_or(EditError::NoTrack(tid))?;
let (_, next_start) = neighbours(tr, *clip);
@@ -801,7 +803,9 @@ pub fn set_speed_group(seq: &mut Sequence, clips: &[ClipId], speed: f64, reverse
let from = member.unwrap_or(main_from);
for i in &mut tr.items {
if !clips.contains(&i.id) && i.start >= from {
- i.start += shift;
+ // later clips must still end on the representable timeline
+ let start = i.start.0.checked_add(shift.0).filter(|s| s.checked_add(i.duration.0).is_some());
+ i.start = Tick(start.ok_or_else(|| EditError::Other("the speed change would move later clips past the end of the timeline".into()))?);
}
}
tr.sort();
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index aea3ceead..7e9ad6454 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -405,3 +405,21 @@ fn merge(v: Vec<(i64, i64)>) -> Vec<(i64, i64)> {
fn unused_kind_import() {
let _ = TrackKind::Video;
}
+
+#[test]
+fn hostile_speeds_are_refused_or_clamped_never_overflow() {
+ // a tiny speed used to saturate the new duration to i64::MAX, then the ripple shift overflowed
+ for speed in [1e-7, 1e-300, f64::MIN_POSITIVE, f64::NAN, f64::INFINITY, -1.0, 0.0] {
+ let mut fx = Fx::new();
+ let v1 = fx.v(0);
+ let a = fx.put(v1, 0, 10, 10);
+ let _b = fx.put(v1, 10, 10, 10);
+ let before = fx.seq.clone();
+ let mut n = fx.next;
+ let r = set_speed_group(&mut fx.seq, &[a], speed, false, true, &mut Fx::ctx(&mut n));
+ match r {
+ Ok(()) => fx.seq.check().unwrap(),
+ Err(_) => assert_eq!(format!("{:?}", fx.seq), format!("{before:?}"), "{speed}: a refused edit changes nothing"),
+ }
+ }
+}