Don't crash from incomplete wav files

This commit is contained in:
Henrik Rydgård 2023-09-27 00:07:35 +02:00
parent 4c0077fd84
commit cbbaf148f4

View File

@ -148,6 +148,7 @@ void WavData::Read(RIFFReader &file_) {
raw_data = (uint8_t *)malloc(numBytes);
raw_data_size = numBytes;
if (num_channels == 1 || num_channels == 2) {
file_.ReadData(raw_data, numBytes);
} else {
@ -410,7 +411,11 @@ Sample *Sample::Load(const std::string &path) {
samples[i] = ConvertU8ToI16(wave.raw_data[i]);
}
}
return new Sample(samples, wave.num_channels, wave.numFrames, wave.sample_rate);
// Protect against bad metadata.
int actualFrames = std::min(wave.numFrames, wave.raw_data_size / wave.raw_bytes_per_frame);
return new Sample(samples, wave.num_channels, actualFrames, wave.sample_rate);
}
static inline int16_t Clamp16(int32_t sample) {