From d73052832eb61cf686b3455703e954a5eeb96dce Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 19 May 2026 10:18:37 -0400
Subject: [PATCH] mixer: Fix crash when `MIX_PROP_PLAY_MAX_*` is less than
start position.
Total bytes read from the MIX_Track's audio stream would go negative, and then
many, many things would go wrong.
Fixes #855.
(cherry picked from commit 33fe7b125439c2a993580c22467d434c9fbc738c)
---
src/SDL_mixer.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/src/SDL_mixer.c b/src/SDL_mixer.c
index 0deb5d74..e82a74dd 100644
--- a/src/SDL_mixer.c
+++ b/src/SDL_mixer.c
@@ -242,6 +242,8 @@ static void TrackStopped(MIX_Track *track)
static void ApplyFade(MIX_Track *track, int channels, float *pcm, int frames)
{
+ SDL_assert(frames >= 0);
+
// !!! FIXME: this is probably pretty naive.
if (track->fade_direction == 0) {
@@ -412,6 +414,9 @@ static void SDLCALL TrackGetCallback(void *userdata, SDL_AudioStream *stream, in
const Sint64 newpos = (Sint64)(track->position + frames_read);
if (newpos >= maxpos) { // we read past the end of the fade out or maxframes, we need to clamp.
br -= (int)(((newpos - maxpos) * raw_channels) * sizeof(float));
+ if (br < 0) {
+ br = 0;
+ }
frames_read = br / (sizeof (float) * raw_channels);
end_of_audio = true;
}