From cc830003f5784b6c532318ee05b28ce305516a82 Mon Sep 17 00:00:00 2001 From: Ian Curtis Date: Fri, 22 Feb 2019 01:00:41 +0000 Subject: [PATCH] Fix possible negative pointer arithmetic which was causing the music to wrap around. --- Src/Sound/MPEG/MpegAudio.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Src/Sound/MPEG/MpegAudio.cpp b/Src/Sound/MPEG/MpegAudio.cpp index 6f92e04..2a49eaa 100644 --- a/Src/Sound/MPEG/MpegAudio.cpp +++ b/Src/Sound/MPEG/MpegAudio.cpp @@ -7,7 +7,7 @@ struct Decoder mp3dec_t mp3d; mp3dec_frame_info_t info; const uint8_t* buffer; - size_t size, pos; + int size, pos; bool loop; bool stopped; int numSamples; @@ -32,11 +32,17 @@ void MpegDec::SetMemory(const uint8_t *data, int length, bool loop) void MpegDec::UpdateMemory(const uint8_t* data, int length, bool loop) { - auto pos = dec.buffer + dec.pos; + int diff; + if (data > dec.buffer) { + diff = (int)(data - dec.buffer); + } + else { + diff = -(int)(dec.buffer - data); + } dec.buffer = data; dec.size = length; - dec.pos = pos - dec.buffer; // update position relative to our new start location + dec.pos = dec.pos - diff; // update position relative to our new start location dec.loop = loop; }