A visualizer byte past a byte is refused, not truncated
Browse files at this commitff23d47e3040602025815794b8a15aac2e39e797Parent: 2d2933c- fix
- a visualizer byte past a byte is refused, not truncated
1 changed file +99 -22
- crates/engine/src/edith.rs +99 -22
crates/engine/src/edith.rsmodified+99 -22
| @@ -387,8 +387,24 @@ pub fn save( | ||
| 387 | 387 | let result = std::fs::File::create(&part) |
| 388 | 388 | .and_then(|mut f| { |
| 389 | 389 | 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, | |
| 392 | 408 | ))?; |
| 393 | 409 | f.sync_all() |
| 394 | 410 | }) |
| @@ -810,10 +826,9 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { | ||
| 810 | 826 | } |
| 811 | 827 | b"samplerate" if v17 => { |
| 812 | 828 | 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 | + ); | |
| 817 | 832 | } |
| 818 | 833 | let f = fields(rest, 1, "samplerate", n)?; |
| 819 | 834 | let rate = number(f[0], n)?; |
| @@ -838,9 +853,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { | ||
| 838 | 853 | } |
| 839 | 854 | b"proxy" if v12 => { |
| 840 | 855 | 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()); | |
| 844 | 857 | } |
| 845 | 858 | let f = fields(rest, 1, "proxy", n)?; |
| 846 | 859 | doc.proxy = match f[0] { |
| @@ -877,7 +890,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { | ||
| 877 | 890 | b"encoder" if v14 => { |
| 878 | 891 | if !doc.sources.is_empty() { |
| 879 | 892 | return Err( |
| 880 | - format!("line {n}: encoder belongs once, before the sources").into() | |
| 893 | + format!("line {n}: encoder belongs once, before the sources").into(), | |
| 881 | 894 | ); |
| 882 | 895 | } |
| 883 | 896 | let f = fields(rest, 1, "encoder", n)?; |
| @@ -1215,7 +1228,7 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { | ||
| 1215 | 1228 | fade_in: number(f.get(9).copied().unwrap_or(b"0"), n)?.min(frames), |
| 1216 | 1229 | fade_out: number(f.get(10).copied().unwrap_or(b"0"), n)?.min(frames), |
| 1217 | 1230 | 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)?, | |
| 1219 | 1232 | viz_paint: number64(f.get(14).copied().unwrap_or(b"0"), n)?, |
| 1220 | 1233 | start: number(f[0], n)?, |
| 1221 | 1234 | in_frame, |
| @@ -1472,6 +1485,15 @@ fn float(field: &[u8], line: usize) -> crate::Result<f32> { | ||
| 1472 | 1485 | } |
| 1473 | 1486 | } |
| 1474 | 1487 | |
| 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 | + | |
| 1475 | 1497 | fn number(field: &[u8], line: usize) -> crate::Result<u32> { |
| 1476 | 1498 | match std::str::from_utf8(field).ok().and_then(|s| s.parse().ok()) { |
| 1477 | 1499 | Some(n) => Ok(n), |
| @@ -1695,7 +1717,11 @@ mod tests { | ||
| 1695 | 1717 | (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(7))]), |
| 1696 | 1718 | (LaneKind::Subtitle, Vec::new()), |
| 1697 | 1719 | ]; |
| 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 | + ]; | |
| 1699 | 1725 | let bytes = super::emit( |
| 1700 | 1726 | &dir, |
| 1701 | 1727 | &doc().1, |
| @@ -1917,7 +1943,10 @@ mod tests { | ||
| 1917 | 1943 | for seat in [EncoderSeat::Hardware, EncoderSeat::Software] { |
| 1918 | 1944 | let written = bytes(seat); |
| 1919 | 1945 | 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 | + ); | |
| 1921 | 1950 | assert_eq!(parse(&written, &dir).expect("v14 parses").encoder, seat); |
| 1922 | 1951 | } |
| 1923 | 1952 | |
| @@ -2622,7 +2651,10 @@ mod tests { | ||
| 2622 | 2651 | "the fades are the clip line's last two fields" |
| 2623 | 2652 | ); |
| 2624 | 2653 | 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 | + ); | |
| 2626 | 2658 | } |
| 2627 | 2659 | |
| 2628 | 2660 | /// The whole of the v19 bump, the fade test's twin: a clip's dissolve into |
| @@ -3248,13 +3280,11 @@ mod tests { | ||
| 3248 | 3280 | assert!(err.starts_with("line 4: clip at "), "{err}"); |
| 3249 | 3281 | } |
| 3250 | 3282 | // 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()); | |
| 3258 | 3288 | // ...and a lane may be empty as long as the other one is not. |
| 3259 | 3289 | let only_audio = parse(b"edith 2\nsource a.mp4\naudio 0 0 30 0 -\n", &dir).expect("parse"); |
| 3260 | 3290 | assert!(only_audio.lanes[0].1.is_empty(), "V1 is there and empty"); |
| @@ -3457,7 +3487,10 @@ mod tests { | ||
| 3457 | 3487 | parse(b"edith 1\nsource a.mp4\n", &dir).expect("v1").lanes, |
| 3458 | 3488 | two(Vec::new(), Vec::new()) |
| 3459 | 3489 | ); |
| 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()); | |
| 3461 | 3494 | } |
| 3462 | 3495 | |
| 3463 | 3496 | /// Every truncation of a good file either parses or refuses; none panics. |
| @@ -3479,4 +3512,48 @@ mod tests { | ||
| 3479 | 3512 | "line 2: truncated % escape in the path" |
| 3480 | 3513 | ); |
| 3481 | 3514 | } |
| 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 | + } | |
| 3482 | 3559 | } |