forge
Explore
Log in

Don't you dare go Hollow

Refuse a 4th substream at parse time, not after

Browse files at this commit

969dce140798d8984abb3dc2c7b87402885fd51dAhmet Kaan GümüşParent: 4026107
  • fix
    • refuse a 4th substream at parse time, not after
Show full message
fix(truehd-sync): refuse a 4th substream at parse time, not after

The >=4 substream guard sat at the top of the directory loop, checked
against the count from the previous iteration, so it only fired on a
5th entry. A well-formed 4-entry directory (the real object-audio
shape) whose last pointer landed on the AU end broke out of the loop
first, parsing Ok(4); only channel_layout() later caught it, and as a
generic Corrupt rather than naming the 4th substream as Unsupported.

Move the check to after the push, so any 4th entry is refused by name
regardless of whether it also terminates the directory.
crates/ec-truehd/src/sync.rsmodified+37 -6
@@ -218,12 +218,6 @@ impl AccessUnitHeader {
218 let mut substreams = Vec::new();218 let mut substreams = Vec::new();
219 let mut pos = dir_start;219 let mut pos = dir_start;
220 loop {220 loop {
221- if substreams.len() >= 4 {
222- return Err(Error::unsupported(
223- "TrueHD 4th (object/16-channel) substream",
224- "only the 2/6/8-channel PCM presentations are implemented",
225- ));
226- }
227 let word = au221 let word = au
228 .get(pos..pos + 2)222 .get(pos..pos + 2)
229 .ok_or(Error::NeedMore)?;223 .ok_or(Error::NeedMore)?;
@@ -243,6 +237,12 @@ impl AccessUnitHeader {
243 end_offset_words,237 end_offset_words,
244 extra,238 extra,
245 });239 });
240+ if substreams.len() >= 4 {
241+ return Err(Error::unsupported(
242+ "TrueHD 4th (object/16-channel) substream",
243+ "only the 2/6/8-channel PCM presentations are implemented",
244+ ));
245+ }
246 if pos + usize::from(end_offset_words) * 2 == length {246 if pos + usize::from(end_offset_words) * 2 == length {
247 break;247 break;
248 }248 }
@@ -465,6 +465,37 @@ mod tests {
465 assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");465 assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");
466 }466 }
467 467
468+ #[test]
469+ fn a_well_formed_4_entry_directory_is_still_named_unsupported() {
470+ // A 20-byte access unit whose directory has exactly 4 entries, the
471+ // last of which lands squarely on the access unit's end (the
472+ // well-formed object-audio shape, not a walk that overruns a
473+ // malformed directory). This must be refused by name, not parsed as
474+ // Ok(4) and only caught later in channel_layout().
475+ let mut au = vec![0x00, 0x0A, 0x00, 0x00]; // length 10 words = 20 bytes
476+ au.extend_from_slice(&[0x00, 0x01]); // substream 0: doesn't land on EOF yet
477+ au.extend_from_slice(&[0x00, 0x01]); // substream 1: doesn't land on EOF yet
478+ au.extend_from_slice(&[0x00, 0x01]); // substream 2: doesn't land on EOF yet
479+ au.extend_from_slice(&[0x00, 0x04]); // substream 3: pos(12) + 4*2 == 20, lands on EOF
480+ au.resize(20, 0);
481+ let err = AccessUnitHeader::parse(&au).unwrap_err();
482+ assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");
483+ }
484+
485+ #[test]
486+ fn a_well_formed_3_entry_directory_still_parses() {
487+ // Control for the test above: the same shape one entry shorter, its
488+ // last pointer landing on the access unit's end, must still parse.
489+ let mut au = vec![0x00, 0x0A, 0x00, 0x00]; // length 10 words = 20 bytes
490+ au.extend_from_slice(&[0x00, 0x01]); // substream 0: doesn't land on EOF yet
491+ au.extend_from_slice(&[0x00, 0x01]); // substream 1: doesn't land on EOF yet
492+ au.extend_from_slice(&[0x00, 0x05]); // substream 2: pos(10) + 5*2 == 20, lands on EOF
493+ au.resize(20, 0);
494+ let parsed = AccessUnitHeader::parse(&au).unwrap();
495+ assert_eq!(parsed.substreams.len(), 3);
496+ assert_eq!(parsed.channel_layout().unwrap(), ChannelLayout::Surround7_1);
497+ }
498+
468 #[test]499 #[test]
469 fn decoder_yields_nothing_before_a_major_sync() {500 fn decoder_yields_nothing_before_a_major_sync() {
470 let mut decoder = TrueHdDecoder::new();501 let mut decoder = TrueHdDecoder::new();