fix: seqLoadStatus OOB reads/writes for custom SAF sequences (#5706) - #6917
Merged
Conversation
serprex
self-requested a review
July 13, 2026 02:27
Custom SAF sequence IDs can exceed sequenceMapSize, causing out-of-bounds reads/writes on the seqLoadStatus byte array (sized exactly sequenceMapSize). This is the direct cause of intermittent battle music failure with BGM packs (issue HarbourMasters#5706): on the second encounter, AudioLoad_SyncLoadSeq reads a garbage value from heap memory past seqLoadStatus[] and may find 1 (loading in progress), causing it to return NULL early — the sequence player is never initialized and no battle music plays. Symmetric fix to the fontLoadStatus guards in PR HarbourMasters#6916: - AudioLoad_IsSeqLoadComplete: return true for OOB seqIds (custom SAF sequences are resource-manager-backed, not in the async-load status table) - AudioLoad_SetSeqLoadStatus: skip update for seqId >= sequenceMapSize - AudioLoad_SyncLoadSeq: skip the in-progress check for OOB seqIds Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
bassdr
force-pushed
the
fix/saf-battle-music
branch
from
August 3, 2026 18:53
799e1bf to
cec3eab
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
With a custom music pack (BGM replacement) installed, battle music often fails to play — the first enemy encounter might work, but re-approaching enemies frequently gives silence. Fixes #5706.
Why it happens
The audio engine keeps a small "is this sequence loaded yet?" status array (
seqLoadStatus). It is sized for the game's built-in sequences only.Custom sequences loaded from a pack are given ID numbers that start past the end of that array. But three spots in the loader still index the array with the sequence's ID directly. When the ID belongs to a custom sequence, that index lands past the end of the array, in unrelated memory — an out-of-bounds (OOB) access:
That garbage read is what breaks battle music: if the leftover byte happens to look like "still loading," the loader gives up early and the sequence player is never started, so no music plays. Because the value depends on whatever was previously in that memory, the failure is intermittent — which is why it usually works the first time and fails on later encounters.
The fix
Custom sequences aren't tracked in that status array at all — they're served on demand by the resource manager and are always ready. So the fix simply teaches those three spots to recognize an out-of-range ID and skip the array:
This mirrors the already-merged fix for the font status array (#6916), which was the same bug on the sound-font side.
Test plan
Build Artifacts