Skip to content

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

#856: Reject negative times on timeline.place and markers.add

Vikram Bhaskaranb964b9970a82
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 6609afddf..6f7d25226 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -239,6 +239,15 @@ pub(crate) fn time_p(s: &Session, p: &Value, prefix: &str) -> Option<Tick> {
None
}
+/// Sequence time from params. Times before 0 are unreachable in the GUI and cut off on export.
+pub(crate) fn sequence_time_p(s: &Session, p: &Value, prefix: &str, cmd: &str) -> Result<Option<Tick>> {
+ let Some(t) = time_p(s, p, prefix) else { return Ok(None) };
+ if t < Tick::ZERO {
+ return Err(bad(cmd, "time must not be negative"));
+ }
+ Ok(Some(t))
+}
+
pub(crate) fn clip_p(p: &Value, k: &str) -> Option<ClipId> {
u64_p(p, k).map(ClipId)
}
@@ -1999,7 +2008,7 @@ fn build() -> Vec<CommandSpec> {
return Ok(v);
}
let (_seq, ph) = marker_list_mut(s)?;
- let t = time_p(s, p, "").unwrap_or(ph);
+ let t = sequence_time_p(s, p, "", "markers.add")?.unwrap_or(ph);
let name = str_p(p, "name").unwrap_or("").to_string();
let comment = str_p(p, "comment").unwrap_or("").to_string();
let color = str_p(p, "color").and_then(Label::from_name).unwrap_or(Label::Green);
@@ -2200,7 +2209,7 @@ fn build() -> Vec<CommandSpec> {
return Err(bad("timeline.place", "enable video, audio, or both"));
}
let item = item_p(p, "item").ok_or_else(|| bad("timeline.place", "need `item`"))?;
- let at = time_p(s, p, "").unwrap_or(s.playhead());
+ let at = sequence_time_p(s, p, "", "timeline.place")?.unwrap_or(s.playhead());
let tg = s.targeting();
let pi = s.project.item(item).ok_or_else(|| bad("timeline.place", "no such item"))?;
let is_audio = |t: TrackId| s.active_sequence().is_some_and(|q| q.audio_tracks.iter().any(|x| x.id == t));
diff --git a/crates/engine/src/sequence_extras.rs b/crates/engine/src/sequence_extras.rs
index 389590c81..8b5d8e6d8 100644
--- a/crates/engine/src/sequence_extras.rs
+++ b/crates/engine/src/sequence_extras.rs
@@ -20,7 +20,7 @@ use filmcraft_project::{ClipId, ItemKind, Label, Marker, MarkerId, MarkerKind, S
use filmcraft_time::{Tick, TimeRange};
use serde_json::{Value, json};
-use crate::commands::{CommandSpec, bad, bool_p, f64_p, has_seq, str_p, time_p, u64_p};
+use crate::commands::{CommandSpec, bad, bool_p, f64_p, has_seq, sequence_time_p, str_p, u64_p};
use crate::{EngineError, Result, Session};
type Run = fn(&mut Session, &Value) -> Result<Value>;
@@ -483,7 +483,7 @@ fn show_active_only(s: &mut Session, p: &Value) -> Result<Value> {
// ---------------------------------------------------------------------------------------------
fn add_flash_cue(s: &mut Session, p: &Value) -> Result<Value> {
- let t = s.sequence_rate().snap(time_p(s, p, "").unwrap_or(s.playhead()));
+ let t = s.sequence_rate().snap(sequence_time_p(s, p, "", "markers.addFlashCue")?.unwrap_or(s.playhead()));
let n = s.active_sequence().map(|q| q.markers.iter().filter(|m| m.kind == MarkerKind::FlashCue).count()).unwrap_or(0) + 1;
let name = str_p(p, "name").map(str::to_string).unwrap_or_else(|| format!("Cue Point {n}"));
let comment = str_p(p, "comment").unwrap_or("").to_string();
diff --git a/crates/engine/src/sequence_tools.rs b/crates/engine/src/sequence_tools.rs
index dc6c57953..897b91125 100644
--- a/crates/engine/src/sequence_tools.rs
+++ b/crates/engine/src/sequence_tools.rs
@@ -31,7 +31,7 @@ use filmcraft_project::{AudioChannels, ClipId, ItemId, Label, Marker, MarkerId,
use filmcraft_time::{Tick, TimeRange};
use serde_json::{Value, json};
-use crate::commands::{CommandSpec, always, bad, bool_p, clips_p, has_seq, str_p, time_p, track_p, with_links};
+use crate::commands::{CommandSpec, always, bad, bool_p, clips_p, has_seq, sequence_time_p, str_p, time_p, track_p, with_links};
use crate::{EngineError, Result, Session};
type Run = fn(&mut Session, &Value) -> Result<Value>;
@@ -206,7 +206,7 @@ fn groups() -> Vec<(&'static str, Vec<CommandSpec>)> {
r#"{"time":ticks?,"name":str?,"comment":str?,"color":label?}"#,
has_seq,
|s, p| {
- let t = time_p(s, p, "").unwrap_or(s.playhead());
+ let t = sequence_time_p(s, p, "", "markers.addChapter")?.unwrap_or(s.playhead());
let n = s.active_sequence().map(|q| q.markers.iter().filter(|m| m.kind == MarkerKind::Chapter).count()).unwrap_or(0) + 1;
let name = str_p(p, "name").map(str::to_string).unwrap_or_else(|| format!("Chapter {n}"));
let id = add_marker(s, "Add Chapter Marker", t, Tick::ZERO, MarkerKind::Chapter, name, p)?;
@@ -977,7 +977,7 @@ fn add_marker(s: &mut Session, label: &str, t: Tick, dur: Tick, kind: MarkerKind
/// `duration` say otherwise) at the playhead.
fn add_range_marker(s: &mut Session, p: &Value) -> Result<Value> {
let rate = s.sequence_rate();
- let t = time_p(s, p, "").unwrap_or(s.playhead());
+ let t = sequence_time_p(s, p, "", "markers.addRange")?.unwrap_or(s.playhead());
let dur = p
.get("duration")
.and_then(Value::as_i64)
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 9852734d7..3b01ca7db 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -958,3 +958,34 @@ fn enable_flips_each_selected_clip() {
s.execute("clip.enable", json!({"clips": [a, b]})).unwrap();
assert_eq!((enabled(&s, a), enabled(&s, b)), (true, true));
}
+
+/// Times before sequence start are unreachable in the GUI and cut off on export.
+#[test]
+fn negative_place_and_marker_times_are_refused() {
+ let mut s = demo();
+ let item = s.active_sequence().unwrap().video_tracks[0].items[0].item;
+ let before = s.project.clone();
+ let n_markers = s.active_sequence().unwrap().markers.len();
+ let n_clips: usize = s.active_sequence().unwrap().video_tracks.iter().map(|t| t.items.len()).sum();
+ for (cmd, params) in [
+ ("timeline.place", json!({"item": item.0, "frame": -12})),
+ ("timeline.place", json!({"item": item.0, "seconds": -1.0})),
+ ("timeline.place", json!({"item": item.0, "time": -1})),
+ ("markers.add", json!({"frame": -10})),
+ ("markers.add", json!({"seconds": -0.5})),
+ ("markers.add", json!({"time": i64::MIN})),
+ ("markers.addChapter", json!({"frame": -1})),
+ ("markers.addRange", json!({"time": -1, "durationFrames": 10})),
+ ("markers.addFlashCue", json!({"frame": -3})),
+ ] {
+ let e = s.execute(cmd, params.clone()).expect_err(&format!("{cmd} {params}"));
+ assert!(matches!(&e, EngineError::BadParams { cmd: c, msg } if c == cmd && msg.contains("negative")), "{cmd} {params}: {e:?}");
+ assert_eq!(*s.project, *before);
+ }
+ assert_eq!(s.active_sequence().unwrap().markers.len(), n_markers);
+ let n_clips_after: usize = s.active_sequence().unwrap().video_tracks.iter().map(|t| t.items.len()).sum();
+ assert_eq!(n_clips_after, n_clips);
+ s.execute("timeline.place", json!({"item": item.0, "frame": 0})).unwrap();
+ s.execute("markers.add", json!({"frame": 0, "name": "start"})).unwrap();
+ assert!(s.active_sequence().unwrap().markers.iter().any(|m| m.start == Tick::ZERO && m.name == "start"));
+}