Fixed crash with very short music loop

https://forum.zdoom.org/viewtopic.php?t=59883
This commit is contained in:
alexey.lysiuk 2018-03-21 12:28:12 +02:00
parent fae6f22a2b
commit d58169f1b0

View file

@ -420,12 +420,17 @@ bool SndFileSong::Read(SoundStream *stream, void *vbuff, int ilen, void *userdat
// This looks a bit more complicated than necessary because libmpg123 will not read the full requested length for the last block in the file. // This looks a bit more complicated than necessary because libmpg123 will not read the full requested length for the last block in the file.
if (currentpos + framestoread > song->Loop_End) if (currentpos + framestoread > song->Loop_End)
{ {
size_t endblock = (song->Loop_End - currentpos) * song->Channels * 2; // Loop can be very short, make sure the current position doesn't exceed it
size_t endlen = song->Decoder->read(buff, endblock); if (currentpos < song->Loop_End)
{
size_t endblock = (song->Loop_End - currentpos) * song->Channels * 2;
size_t endlen = song->Decoder->read(buff, endblock);
// Even if zero bytes was read give it a chance to start from the beginning
buff += endlen;
len -= endlen;
}
// Even if zero bytes was read give it a chance to start from the beginning
buff = buff + endlen;
len -= endlen;
song->Decoder->seek(song->Loop_Start, false, true); song->Decoder->seek(song->Loop_Start, false, true);
} }
while (len > 0) while (len > 0)