From 79193b914bdd2221b7d5222ab64750d54fccb339 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 25 Jun 2024 04:08:47 -0700 Subject: [PATCH] GBA Audio: Fix crash in audio subsampling if timing lockstep breaks --- CHANGES | 1 + include/mgba/internal/gba/audio.h | 2 +- src/gba/audio.c | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index c73b2528b..a0b87e214 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,7 @@ Other fixes: - Debugger: Fix writing to specific segment in command-line debugger - GB: Fix uninitialized save data when loading undersized temporary saves - GBA Audio: Fix crash if audio FIFOs and timers get out of sync + - GBA Audio: Fix crash in audio subsampling if timing lockstep breaks - GBA Memory: Let raw access read high MMIO addresses - Qt: Fix savestate preview sizes with different scales (fixes mgba.io/i/2560) - Qt: Fix potential crash when configuring shortcuts diff --git a/include/mgba/internal/gba/audio.h b/include/mgba/internal/gba/audio.h index 5b5db3a62..fba5090bf 100644 --- a/include/mgba/internal/gba/audio.h +++ b/include/mgba/internal/gba/audio.h @@ -80,7 +80,7 @@ struct GBAAudio { int32_t sampleInterval; int32_t lastSample; - int sampleIndex; + unsigned sampleIndex; struct mStereoSample currentSamples[GBA_MAX_SAMPLES]; bool forceDisableChA; diff --git a/src/gba/audio.c b/src/gba/audio.c index 25e400b19..a6ebafbae 100644 --- a/src/gba/audio.c +++ b/src/gba/audio.c @@ -232,6 +232,9 @@ void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value) { if (oldSampleInterval != audio->sampleInterval) { timestamp -= audio->lastSample; audio->sampleIndex = timestamp >> (9 - GBARegisterSOUNDBIASGetResolution(value)); + if (audio->sampleIndex >= GBA_MAX_SAMPLES) { + audio->sampleIndex = 0; + } if (audio->p->stream && audio->p->stream->audioRateChanged) { audio->p->stream->audioRateChanged(audio->p->stream, GBA_ARM7TDMI_FREQUENCY / audio->sampleInterval); }