From d58169f1b028df0b5dd36b24c14464a155c529d1 Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Wed, 21 Mar 2018 12:28:12 +0200 Subject: [PATCH] Fixed crash with very short music loop https://forum.zdoom.org/viewtopic.php?t=59883 --- src/sound/musicformats/music_libsndfile.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/sound/musicformats/music_libsndfile.cpp b/src/sound/musicformats/music_libsndfile.cpp index 3a44d9f56..61be6b56c 100644 --- a/src/sound/musicformats/music_libsndfile.cpp +++ b/src/sound/musicformats/music_libsndfile.cpp @@ -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. if (currentpos + framestoread > song->Loop_End) { - size_t endblock = (song->Loop_End - currentpos) * song->Channels * 2; - size_t endlen = song->Decoder->read(buff, endblock); + // Loop can be very short, make sure the current position doesn't exceed it + 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); } while (len > 0)