From 6bf4e8013004b21c535f8fcaca5447fd388b95d8 Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Sat, 30 May 2026 22:01:32 +0800 Subject: [PATCH] silently discard trailing partial sample byte(s) Signed-off-by: JaySon-Huang --- src/read.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/read.rs b/src/read.rs index 5f8e25b..4831529 100644 --- a/src/read.rs +++ b/src/read.rs @@ -599,15 +599,11 @@ impl WavReader let num_samples = data_len / spec_ex.bytes_per_sample as u32; - // It could be that num_samples * bytes_per_sample < data_len. - // If data_len is not a multiple of bytes_per_sample, there is some - // trailing data. Either somebody is playing some steganography game, - // but more likely something is very wrong, and we should refuse to - // decode the file, as it is invalid. - if num_samples * spec_ex.bytes_per_sample as u32 != data_len { - let msg = "data chunk length is not a multiple of sample size"; - return Err(Error::FormatError(msg)); - } + // If data_len is not a multiple of bytes_per_sample, the trailing + // partial sample byte(s) are silently discarded. The integer division + // above already truncates to the last complete sample frame. + // This tolerates WAV files where the last byte of a multi-byte sample + // was lost (e.g., truncated file transfers). // The number of samples must be a multiple of the number of channels, // otherwise the last inter-channel sample would not have data for all