[MAD Decoder] Fix sample count calculation crash

This condition would underflow when skipping a bunch of samples on the
start of playback, or otherwise seeking, and could cause an unsigned
underflow, which would cause the subsequent vDSP_vflt32 to overread into
the MAD sample buffer and crash.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-06-10 02:44:02 -07:00
parent fe82b5ed65
commit ffdb6262c2
1 changed files with 5 additions and 1 deletions

View File

@ -436,8 +436,12 @@
return;
}
// Clip this for the following calculation, so this doesn't underflow
// when seeking and skipping a lot of samples
unsigned long startingSampleClipped = MIN(startingSample, sampleCount);
// We are at the end of the file and need to read the last few frames
if(_framesDecoded + (sampleCount - startingSample) > totalFrames - _endPadding) {
if(_framesDecoded + (sampleCount - startingSampleClipped) > totalFrames - _endPadding) {
// DLog(@"End of file. %li", totalFrames - _endPadding - _framesDecoded);
sampleCount = totalFrames - _endPadding - _framesDecoded + startingSample;
}