PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix: limit Render Video timeline and GIF staging allocations (#5670)
Nigel Ihza Firdausy5c78d0848ff0
diff --git a/crates/engine/src/timeline_cmds.rs b/crates/engine/src/timeline_cmds.rsindex 197c8e9cc..9baa07760 100644--- a/crates/engine/src/timeline_cmds.rs+++ b/crates/engine/src/timeline_cmds.rs@@ -12,7 +12,7 @@ use crate::{EngineError, Result, Session};/// Longest timeline, in frames: every frame becomes a surface in a new video layer and a file in/// Render Video, so `duration` is capped well before it can exhaust memory or overflow (#1009).-const MAX_DURATION: usize = 1_000_000;+pub(crate) const MAX_DURATION: usize = 1_000_000;/// Highest frame rate; larger values only come from mistakes and become `inf` as `f32` (#1009).const MAX_FPS: f32 = 1000.0;diff --git a/crates/engine/src/video_cmds.rs b/crates/engine/src/video_cmds.rsindex 2a9f8ae8b..dcb184c67 100644--- a/crates/engine/src/video_cmds.rs+++ b/crates/engine/src/video_cmds.rs@@ -11,6 +11,10 @@ use serde_json::{Value, json};use crate::commands::CommandSpec;use crate::{EngineError, Result, Session};+/// Maximum dense RGBA staging pixels in one animated GIF frame. A GIF also keeps the+/// quantised frame and palette while rendering, so the document canvas limit is not enough.+const MAX_GIF_FRAME_PIXELS: u64 = 64 << 20;+/// Set every video layer's displayed content to its frame at the playhead.pub fn sync(doc: &mut Document) {let cur = doc.timeline.as_ref().map_or(0, |t| t.current);@@ -416,6 +420,13 @@ pub fn render_video_with(let mut base = (*st.doc).clone();store(&mut base);let frames = base.timeline.as_ref().map_or(1, |t| t.duration).max(1);+ // Loaded timelines bypass the duration validation used by timeline.create/setProps.+ if frames > crate::timeline_cmds::MAX_DURATION {+ return Err(EngineError::Other(format!(+ "the timeline is {frames} frames; Render Video is limited to {} frames",+ crate::timeline_cmds::MAX_DURATION+ )));+ }let fps = base.timeline.as_ref().map_or(30.0, |t| t.fps).max(1.0);let stem = base.name.rsplit_once('.').map_or(base.name.as_str(), |(a, _)| a).to_string();let fmt = base.pixel_format();@@ -429,6 +440,13 @@ pub fn render_video_with(// Animated GIF: one file, every frame quantised to its own palette.if format.eq_ignore_ascii_case("gif") {+ // The whole canvas is staged as f32 RGBA for each frame; reject over-budget+ // dimensions before an infallible allocation can abort the process.+ if (w as u64) * (h as u64) > MAX_GIF_FRAME_PIXELS {+ return Err(EngineError::Other(format!(+ "{w} × {h} is too large to render as an animated GIF (limit {MAX_GIF_FRAME_PIXELS} pixels per frame)"+ )));+ }let delay = (100.0 / fps).round().clamp(1.0, 65535.0) as u16;let mut gframes = Vec::with_capacity(frames);for f in 0..frames {diff --git a/crates/engine/src/video_cmds/tests.rs b/crates/engine/src/video_cmds/tests.rsindex 22d639d49..c16c7f8e3 100644--- a/crates/engine/src/video_cmds/tests.rs+++ b/crates/engine/src/video_cmds/tests.rs@@ -498,3 +498,25 @@ fn grow_duration_never_shrinks_and_keeps_the_work_area_valid() {grow_duration(&mut t, 12);assert_eq!((t.work_start, t.work_end, t.duration), (0, 6, 12));}++#[test]+fn render_video_rejects_a_loaded_timeline_past_the_frame_cap() {+ let mut s = session();+ let mut doc = (*s.active().unwrap().doc).clone();+ doc.timeline = Some(Timeline::new(usize::MAX, 30.0));+ {+ let st = s.active_mut().unwrap();+ st.doc = std::sync::Arc::new(doc);+ st.revision += 1;+ }+ let err = s.execute("file.export.renderVideo", json!({"dir": tmpdir("overlong"), "format": "gif"})).unwrap_err();+ assert!(err.to_string().contains("limited to"), "{err}");+}++#[test]+fn render_video_rejects_gif_frames_too_large_to_stage() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 9000, "height": 8000})).unwrap();+ let err = s.execute("file.export.renderVideo", json!({"dir": tmpdir("large-gif"), "format": "gif"})).unwrap_err();+ assert!(err.to_string().contains("too large"), "{err}");+}