forge
Explore
Log in

Don't you dare go Hollow

A visualizer byte past a byte is refused, not truncated

Browse files at this commit

ff23d47e3040602025815794b8a15aac2e39e797Ahmet Kaan GümüşParent: 2d2933c
  • fix
    • a visualizer byte past a byte is refused, not truncated
Show full message
🐛 fix(edith): a visualizer byte past a byte is refused, not truncated

A clip line naming a look of 300 loaded as 44, with the fast bit and the ink nibble both wrong, while the module's own rule is that every bad field is refused by name beside the resolution bound, the sample rate range, the tone map and the encoder seat.
crates/engine/src/edith.rsmodified+99 -22
@@ -387,8 +387,24 @@ pub fn save(
387387 let result = std::fs::File::create(&part)
388388 .and_then(|mut f| {
389389 f.write_all(&emit(
390- &dir, sources, lanes, gains, subs, subtitles, eq, color, transform, resolution,
391- fps, tone, proxy, auto_proxy, encoder, limiter, sample_rate, playhead,
390+ &dir,
391+ sources,
392+ lanes,
393+ gains,
394+ subs,
395+ subtitles,
396+ eq,
397+ color,
398+ transform,
399+ resolution,
400+ fps,
401+ tone,
402+ proxy,
403+ auto_proxy,
404+ encoder,
405+ limiter,
406+ sample_rate,
407+ playhead,
392408 ))?;
393409 f.sync_all()
394410 })
@@ -810,10 +826,9 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
810826 }
811827 b"samplerate" if v17 => {
812828 if doc.sample_rate.is_some() || !doc.sources.is_empty() {
813- return Err(format!(
814- "line {n}: samplerate belongs once, before the sources"
815- )
816- .into());
829+ return Err(
830+ format!("line {n}: samplerate belongs once, before the sources").into(),
831+ );
817832 }
818833 let f = fields(rest, 1, "samplerate", n)?;
819834 let rate = number(f[0], n)?;
@@ -838,9 +853,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
838853 }
839854 b"proxy" if v12 => {
840855 if !doc.sources.is_empty() {
841- return Err(
842- format!("line {n}: proxy belongs once, before the sources").into()
843- );
856+ return Err(format!("line {n}: proxy belongs once, before the sources").into());
844857 }
845858 let f = fields(rest, 1, "proxy", n)?;
846859 doc.proxy = match f[0] {
@@ -877,7 +890,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
877890 b"encoder" if v14 => {
878891 if !doc.sources.is_empty() {
879892 return Err(
880- format!("line {n}: encoder belongs once, before the sources").into()
893+ format!("line {n}: encoder belongs once, before the sources").into(),
881894 );
882895 }
883896 let f = fields(rest, 1, "encoder", n)?;
@@ -1215,7 +1228,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
12151228 fade_in: number(f.get(9).copied().unwrap_or(b"0"), n)?.min(frames),
12161229 fade_out: number(f.get(10).copied().unwrap_or(b"0"), n)?.min(frames),
12171230 transition_out: number(f.get(11).copied().unwrap_or(b"0"), n)?.min(frames),
1218- visualizer: number(f.get(13).copied().unwrap_or(b"0"), n)? as u8,
1231+ visualizer: flags_byte(f.get(13).copied().unwrap_or(b"0"), n)?,
12191232 viz_paint: number64(f.get(14).copied().unwrap_or(b"0"), n)?,
12201233 start: number(f[0], n)?,
12211234 in_frame,
@@ -1472,6 +1485,15 @@ fn float(field: &[u8], line: usize) -> crate::Result<f32> {
14721485 }
14731486 }
14741487
1488+/// A clip's visualizer field: every bit of the byte is a flag
1489+/// ([`crate::decode::VIZ_STYLE_FILL`]'s low bits, the fast bit, the ink
1490+/// nibble), so a line naming one past what a byte holds is a look this
1491+/// program does not have rather than a 300 that quietly loads as a 44.
1492+fn flags_byte(field: &[u8], line: usize) -> crate::Result<u8> {
1493+ let n = number(field, line)?;
1494+ u8::try_from(n).map_err(|_| format!("line {line}: {n} is not a set of visualizer flags").into())
1495+}
1496+
14751497 fn number(field: &[u8], line: usize) -> crate::Result<u32> {
14761498 match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) {
14771499 Some(n) => Ok(n),
@@ -1695,7 +1717,11 @@ mod tests {
16951717 (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(7))]),
16961718 (LaneKind::Subtitle, Vec::new()),
16971719 ];
1698- let subs = vec![vec![], vec![], vec![caption(0, 30, Some(7)), caption(40, 10, None)]];
1720+ let subs = vec![
1721+ vec![],
1722+ vec![],
1723+ vec![caption(0, 30, Some(7)), caption(40, 10, None)],
1724+ ];
16991725 let bytes = super::emit(
17001726 &dir,
17011727 &doc().1,
@@ -1917,7 +1943,10 @@ mod tests {
19171943 for seat in [EncoderSeat::Hardware, EncoderSeat::Software] {
19181944 let written = bytes(seat);
19191945 let text = String::from_utf8_lossy(&written).to_string();
1920- assert!(text.contains(&format!("encoder {}\n", seat.name())), "{text}");
1946+ assert!(
1947+ text.contains(&format!("encoder {}\n", seat.name())),
1948+ "{text}"
1949+ );
19211950 assert_eq!(parse(&written, &dir).expect("v14 parses").encoder, seat);
19221951 }
19231952
@@ -2622,7 +2651,10 @@ mod tests {
26222651 "the fades are the clip line's last two fields"
26232652 );
26242653 let back = parse(&bytes, &dir).expect("parse");
2625- assert_eq!(back.lanes, lanes, "the fades round trip as the very numbers");
2654+ assert_eq!(
2655+ back.lanes, lanes,
2656+ "the fades round trip as the very numbers"
2657+ );
26262658 }
26272659
26282660 /// The whole of the v19 bump, the fade test's twin: a clip's dissolve into
@@ -3248,13 +3280,11 @@ mod tests {
32483280 assert!(err.starts_with("line 4: clip at "), "{err}");
32493281 }
32503282 // The lanes are independent, so a video line never crowds an audio one.
3251- assert!(
3252- parse(
3253- b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\naudio 0 0 30 0 -\n",
3254- &dir
3255- )
3256- .is_ok()
3257- );
3283+ assert!(parse(
3284+ b"edith 2\nsource a.mp4\nvideo 0 0 30 0 -\naudio 0 0 30 0 -\n",
3285+ &dir
3286+ )
3287+ .is_ok());
32583288 // ...and a lane may be empty as long as the other one is not.
32593289 let only_audio = parse(b"edith 2\nsource a.mp4\naudio 0 0 30 0 -\n", &dir).expect("parse");
32603290 assert!(only_audio.lanes[0].1.is_empty(), "V1 is there and empty");
@@ -3457,7 +3487,10 @@ mod tests {
34573487 parse(b"edith 1\nsource a.mp4\n", &dir).expect("v1").lanes,
34583488 two(Vec::new(), Vec::new())
34593489 );
3460- assert!(parse(b"edith 1\n", &dir).expect("a project of nothing").sources.is_empty());
3490+ assert!(parse(b"edith 1\n", &dir)
3491+ .expect("a project of nothing")
3492+ .sources
3493+ .is_empty());
34613494 }
34623495
34633496 /// Every truncation of a good file either parses or refuses; none panics.
@@ -3479,4 +3512,48 @@ mod tests {
34793512 "line 2: truncated % escape in the path"
34803513 );
34813514 }
3515+
3516+ /// A clip's visualizer field is a whole byte of flags, and a line naming one
3517+ /// past it used to be truncated into a look nobody chose (`300` loaded as
3518+ /// `44`, with the fast bit and the ink nibble both wrong). It is refused by
3519+ /// name now, like the resolution's bound, the sample rate's range, the tone
3520+ /// map and the encoder seat beside it.
3521+ #[test]
3522+ fn a_visualizer_byte_past_a_byte_is_refused_and_the_legal_ones_still_load() {
3523+ // The door the parser reads that field through: the low bits are the
3524+ // style, bit 3 the fast flag and the high nibble the ink, so every value a
3525+ // byte holds is a look and 256 and up is not one.
3526+ assert_eq!(flags_byte(b"0", 1).expect("zero is the plain look"), 0);
3527+ assert_eq!(flags_byte(b"255", 1).expect("the whole byte is flags"), 255);
3528+ let refused = flags_byte(b"300", 9).expect_err("300 is not a byte of flags");
3529+ assert_eq!(
3530+ refused.to_string(),
3531+ "line 9: 300 is not a set of visualizer flags"
3532+ );
3533+ }
3534+
3535+ /// ...and the same field through the parser, on a document of the newest
3536+ /// dialect: the legal byte is what lands in the clip, the illegal one is the
3537+ /// refusal above rather than a truncation.
3538+ #[test]
3539+ fn a_clip_line_whose_visualizer_is_past_a_byte_loses_the_whole_load() {
3540+ let dir = PathBuf::from("/proj");
3541+ let line = |flags: &str| {
3542+ format!(
3543+ "edith 22\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\n\
3544+ video 1 0 0 30 0 - - - fit 1000 0 0 0 - {flags} 0\n"
3545+ )
3546+ };
3547+ let doc = parse(line("12").as_bytes(), &dir).expect("12 is a look");
3548+ // The keyword is the saver's own for this dialect, so a mismatch here would
3549+ // be this test's line and not the field it is about.
3550+ assert_eq!(doc.lanes[0].1[0].visualizer, 12);
3551+ let refused = parse(line("300").as_bytes(), &dir).expect_err("300 is not a look");
3552+ assert!(
3553+ refused
3554+ .to_string()
3555+ .contains("is not a set of visualizer flags"),
3556+ "{refused}"
3557+ );
3558+ }
34823559 }