Check padding addition for DSDIFF size calculation

This commit is contained in:
el-j
2026-11-18 21:23:33 +01:00
parent 669c839251
commit e20290fb43
2 changed files with 18 additions and 1 deletions
+2 -1
View File
@@ -285,7 +285,8 @@ pub fn write_dsdiff(sample_rate: u32, channels: &[&[u8]], sample_count: u64) ->
// Fixed headers plus four bytes for each channel identifier.
// An odd-sized chunk payload is followed by a pad byte that its size field does not count.
let data_pad = data_size & 1;
let file_size = super::encoded_size(data_size + data_pad, 110 + 4 * num_channels)?;
let padded_size = data_size.checked_add(data_pad).ok_or(Error::MemoryLimitExceeded)?;
let file_size = super::encoded_size(padded_size, 110 + 4 * num_channels)?;
// Build PROP subchunks:
// 1. 'FS ' (4 bytes + 12 chunk header)
+16
View File
@@ -118,3 +118,19 @@ fn dsf_audio_stops_at_the_declared_data_chunk() {
assert_eq!(read_raw(&bytes).unwrap().channel_bytes, expected.channel_bytes);
}
}
#[test]
fn dsdiff_odd_payload_receives_pad_byte() {
let input = [&[0xaau8; 3][..]];
let bytes = encode_dsdiff(2_822_400, &input, 24).unwrap();
let raw = read_raw(&bytes).unwrap();
assert_eq!(raw.channel_bytes[0].len(), 3);
}
#[test]
#[cfg(target_pointer_width = "32")]
fn dsdiff_32bit_padding_overflow_is_rejected() {
let dummy = [&[][..]];
let result = encode_dsdiff(2_822_400, &dummy, 34_359_738_360);
assert_eq!(result, Err(Error::MemoryLimitExceeded));
}