fix(audio): an empty WAV data chunk before a LIST chunk is no audio (#2123) (#2246)

A `data` chunk of size 0 counted as empty only when the RIFF size matched
the file. A writer that appends a `LIST` chunk after writing the RIFF size
leaves it stale, so the reader took the file for an unfinished recording
and decoded the `LIST` chunk's bytes as samples (8 frames of noise instead
of none).

A size-0 `data` chunk is now also empty when whole chunks follow it and the
first one has an id that follows audio (LIST, bext, cue, JUNK, ...). A
recording that was never finalised (PCM after the header) and bytes that
only start like a chunk still decode as audio.
This commit is contained in:
ljkx
2026-11-18 15:30:49 -05:00
committed by GitHub
parent 38429defb8
commit cec20449a2
2 changed files with 76 additions and 2 deletions
+15 -2
View File
@@ -252,12 +252,25 @@ struct DataChunk {
declared: u64,
}
/// Ids of the chunks that commonly follow a `data` chunk: metadata a writer appends after the
/// samples, sometimes without correcting the RIFF size.
const TRAILING_IDS: [&[u8; 4]; 12] = [b"LIST", b"fact", b"id3 ", b"ID3 ", b"cue ", b"JUNK", b"PEAK", b"bext", b"smpl", b"iXML", b"inst", b"PAD "];
/// Bytes of audio a `data` chunk holds: leniently, whatever is present when the size is wrong or
/// the recording was never finalised.
///
/// A size of 0 is an empty chunk when only whole chunks follow it and either the RIFF size is the
/// file's or the first of them has an id that follows audio (#2123: a `LIST` chunk appended after
/// the RIFF size was written). Otherwise it is a recording that was never finalised, and what
/// follows is audio: the bytes of PCM can spell any id, but rarely whole chunks to the end.
fn data_chunk_len(src: &Src<'_>, c: DataChunk) -> u64 {
if c.size32 != 0 || c.is_rf64 {
return c.declared.min(c.remaining);
}
let finished = u64::from(src.le_u32(4).unwrap_or(0)) + 8 == src.size();
let empty = c.size32 == 0 && !c.is_rf64 && finished && only_chunks_follow(src, c.body);
if c.size32 == 0 && !c.is_rf64 && !empty { c.remaining } else { c.declared.min(c.remaining) }
let metadata_follows = src.tag(c.body).is_some_and(|id| TRAILING_IDS.contains(&&id));
let empty = (finished || metadata_follows) && only_chunks_follow(src, c.body);
if empty { 0 } else { c.remaining }
}
/// The sample layout of a `fmt` chunk, `None` for a format we do not read natively.
@@ -73,3 +73,64 @@ fn many_empty_data_chunks_parse_in_linear_time() {
assert_eq!(info.frames, 0);
assert_eq!(decode(&b, Some("wav")).unwrap().1.frames(), 0);
}
/// fmt (mono 16-bit), an empty `data` chunk, then an 8-byte `LIST` chunk (#2123).
fn empty_data_then_list() -> Vec<u8> {
riff(&[chunk(b"fmt ", &fmt16(1, 8000)), chunk(b"data", &[]), chunk(b"LIST", b"INFOabcd")])
}
/// An empty `data` chunk followed by a `LIST` chunk holds no audio, whether the RIFF size counts
/// the `LIST` chunk or was written before it was appended (#2123): the `LIST` chunk's bytes are
/// never decoded as samples.
#[test]
fn an_empty_data_chunk_followed_by_a_list_chunk_is_no_audio_whatever_the_riff_size() {
let correct = empty_data_then_list();
// The RIFF size a writer put down before appending the LIST chunk.
let mut stale = correct.clone();
let before_list = (stale.len() - 8 - 16) as u32;
stale[4..8].copy_from_slice(&before_list.to_le_bytes());
// A RIFF size past the end of the file.
let mut past = correct.clone();
past[4..8].copy_from_slice(&u32::MAX.to_le_bytes());
for (name, file) in [("correct", &correct), ("stale", &stale), ("past the end", &past)] {
assert_eq!(probe(file, Some("wav")).unwrap().frames, 0, "{name} RIFF size: probe");
assert_eq!(decode(file, Some("wav")).unwrap().1.frames(), 0, "{name} RIFF size: decode");
}
}
/// Bytes after an empty `data` chunk that only look like the start of a chunk are a recording that
/// was never finalised: a `LIST` id whose size runs past the file, or a cut-off header, stays
/// audio, as the bytes of PCM can spell any id.
#[test]
fn an_empty_data_chunk_followed_by_a_broken_chunk_header_is_still_audio() {
let base = riff(&[chunk(b"fmt ", &fmt16(1, 8000)), chunk(b"data", &[])]);
let stale = |tail: &[u8]| {
let mut f = base.clone();
f.extend_from_slice(tail);
f
};
// A LIST id whose size runs past the end of the file: 12 bytes, 6 frames.
assert_eq!(decode(&stale(b"LIST\0\x10\0\0INFO"), Some("wav")).unwrap().1.frames(), 6);
// A LIST id with a cut-off size field: 6 bytes, 3 frames.
assert_eq!(decode(&stale(b"LIST\0\0"), Some("wav")).unwrap().1.frames(), 3);
}
/// A WAV with samples in a sized `data` chunk and a `LIST` chunk after it decodes exactly as
/// before: its samples, not the `LIST` chunk.
#[test]
fn a_sized_data_chunk_followed_by_a_list_chunk_decodes_its_samples() {
let samples: Vec<i16> = (0..64).map(|i| i * 300 - 9000).collect();
let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
let file = riff(&[chunk(b"fmt ", &fmt16(1, 8000)), chunk(b"data", &bytes), chunk(b"LIST", b"INFOabcd")]);
let (info, buf) = decode(&file, Some("wav")).unwrap();
assert_eq!(info.frames, 64);
assert_eq!(buf.frames(), 64);
let mut stale = file.clone();
let before_list = (stale.len() - 8 - 16) as u32;
stale[4..8].copy_from_slice(&before_list.to_le_bytes());
let (_, stale_buf) = decode(&stale, Some("wav")).unwrap();
assert_eq!(stale_buf.frames(), 64, "a stale RIFF size does not change a sized data chunk");
assert_eq!(stale_buf, buf, "the same samples");
let expected: Vec<f32> = samples.iter().map(|s| f32::from(*s) / 32768.0).collect();
assert_eq!(buf.interleaved(), expected, "the data chunk's samples");
}