Skip to content

Commit aeaeff2

Browse files
ivan-mogilkoicculus
authored andcommitted
Fix infinite loop in mix_source_buffer() in case of uneven input buffer
This fixes a potential infinite loop, which may occur in two cases: 1. If remaining data in the input is less than `bufferframesize`, in such case `bytesput` resulted in 0, and src->offset never advanced. 2. If remaining data's size is not evenly divided by sizeof(float), the data pointer again will not advance to the end.
1 parent fd7e70a commit aeaeff2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

mojoal.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -1432,12 +1432,15 @@ static ALboolean mix_source_buffer(ALCcontext *ctx, ALsource *src, BufferQueueIt
14321432
if (src->stream) { /* resampling? */
14331433
int mixframes, mixlen, remainingmixframes;
14341434
while ( (((mixlen = SDL_AudioStreamAvailable(src->stream)) / bufferframesize) < framesneeded) && (src->offset < buffer->len) ) {
1435-
const int framesput = (buffer->len - src->offset) / bufferframesize;
1436-
const int bytesput = SDL_min(framesput, 1024) * bufferframesize;
1435+
const int bytesleft = (buffer->len - src->offset);
1436+
/* workaround in case remains are less than bufferframesize */
1437+
const int framesput = (bytesleft + (bufferframesize - 1)) / bufferframesize;
1438+
const int bytesput = SDL_min(SDL_min(framesput, 1024) * bufferframesize, bytesleft);
14371439
FIXME("dynamically adjust frames here?"); /* we hardcode 1024 samples when opening the audio device, too. */
14381440
SDL_AudioStreamPut(src->stream, data, bytesput);
14391441
src->offset += bytesput;
1440-
data += bytesput / sizeof (float);
1442+
/* workaround in case remains are not evenly divided by sizeof (float) */
1443+
data += (bytesput + (sizeof (float) - 1)) / sizeof (float);
14411444
}
14421445

14431446
mixframes = SDL_min(mixlen / bufferframesize, framesneeded);

0 commit comments

Comments
 (0)