From 969dce140798d8984abb3dc2c7b87402885fd51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 01:18:09 +0300 Subject: [PATCH] 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.rs | 43 +++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/ec-truehd/src/sync.rs b/crates/ec-truehd/src/sync.rs index 3c3ddb09..1bee6a65 100644 --- a/crates/ec-truehd/src/sync.rs +++ b/crates/ec-truehd/src/sync.rs @@ -218,12 +218,6 @@ impl AccessUnitHeader { let mut substreams = Vec::new(); let mut pos = dir_start; loop { - if substreams.len() >= 4 { - return Err(Error::unsupported( - "TrueHD 4th (object/16-channel) substream", - "only the 2/6/8-channel PCM presentations are implemented", - )); - } let word = au .get(pos..pos + 2) .ok_or(Error::NeedMore)?; @@ -243,6 +237,12 @@ impl AccessUnitHeader { end_offset_words, extra, }); + if substreams.len() >= 4 { + return Err(Error::unsupported( + "TrueHD 4th (object/16-channel) substream", + "only the 2/6/8-channel PCM presentations are implemented", + )); + } if pos + usize::from(end_offset_words) * 2 == length { break; } @@ -465,6 +465,37 @@ mod tests { assert!(matches!(err, Error::Unsupported { .. }), "{err:?}"); } + #[test] + fn a_well_formed_4_entry_directory_is_still_named_unsupported() { + // A 20-byte access unit whose directory has exactly 4 entries, the + // last of which lands squarely on the access unit's end (the + // well-formed object-audio shape, not a walk that overruns a + // malformed directory). This must be refused by name, not parsed as + // Ok(4) and only caught later in channel_layout(). + let mut au = vec![0x00, 0x0A, 0x00, 0x00]; // length 10 words = 20 bytes + au.extend_from_slice(&[0x00, 0x01]); // substream 0: doesn't land on EOF yet + au.extend_from_slice(&[0x00, 0x01]); // substream 1: doesn't land on EOF yet + au.extend_from_slice(&[0x00, 0x01]); // substream 2: doesn't land on EOF yet + au.extend_from_slice(&[0x00, 0x04]); // substream 3: pos(12) + 4*2 == 20, lands on EOF + au.resize(20, 0); + let err = AccessUnitHeader::parse(&au).unwrap_err(); + assert!(matches!(err, Error::Unsupported { .. }), "{err:?}"); + } + + #[test] + fn a_well_formed_3_entry_directory_still_parses() { + // Control for the test above: the same shape one entry shorter, its + // last pointer landing on the access unit's end, must still parse. + let mut au = vec![0x00, 0x0A, 0x00, 0x00]; // length 10 words = 20 bytes + au.extend_from_slice(&[0x00, 0x01]); // substream 0: doesn't land on EOF yet + au.extend_from_slice(&[0x00, 0x01]); // substream 1: doesn't land on EOF yet + au.extend_from_slice(&[0x00, 0x05]); // substream 2: pos(10) + 5*2 == 20, lands on EOF + au.resize(20, 0); + let parsed = AccessUnitHeader::parse(&au).unwrap(); + assert_eq!(parsed.substreams.len(), 3); + assert_eq!(parsed.channel_layout().unwrap(), ChannelLayout::Surround7_1); + } + #[test] fn decoder_yields_nothing_before_a_major_sync() { let mut decoder = TrueHdDecoder::new();