Skip to content

Commit

Permalink
Fix infinite loop in mix_source_buffer() in case of uneven input buffer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ivan-mogilko committed Jan 11, 2025
1 parent fd7e70a commit afb81c3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions mojoal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1432,12 +1432,15 @@ static ALboolean mix_source_buffer(ALCcontext *ctx, ALsource *src, BufferQueueIt
if (src->stream) { /* resampling? */
int mixframes, mixlen, remainingmixframes;
while ( (((mixlen = SDL_AudioStreamAvailable(src->stream)) / bufferframesize) < framesneeded) && (src->offset < buffer->len) ) {
const int framesput = (buffer->len - src->offset) / bufferframesize;
const int bytesput = SDL_min(framesput, 1024) * bufferframesize;
const int bytesleft = (buffer->len - src->offset);
/* workaround in case remains are less than bufferframesize */
const int framesput = (bytesleft + (bufferframesize - 1)) / bufferframesize;
const int bytesput = SDL_min(SDL_min(framesput, 1024) * bufferframesize, bytesleft);
FIXME("dynamically adjust frames here?"); /* we hardcode 1024 samples when opening the audio device, too. */
SDL_AudioStreamPut(src->stream, data, bytesput);
src->offset += bytesput;
data += bytesput / sizeof (float);
/* workaround in case remains are not evenly divided by sizeof (float) */
data += (bytesput + (sizeof (float) - 1)) / sizeof (float);
}

mixframes = SDL_min(mixlen / bufferframesize, framesneeded);
Expand Down

0 comments on commit afb81c3

Please sign in to comment.