Skip to content

fix: seqLoadStatus OOB reads/writes for custom SAF sequences (#5706) - #6917

Merged
serprex merged 1 commit into
HarbourMasters:developfrom
bassdr:fix/saf-battle-music
Aug 3, 2026
Merged

fix: seqLoadStatus OOB reads/writes for custom SAF sequences (#5706)#6917
serprex merged 1 commit into
HarbourMasters:developfrom
bassdr:fix/saf-battle-music

Conversation

@bassdr

@bassdr bassdr commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

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:

  • one spot writes a status there (corrupting whatever memory happens to sit after the array), and
  • two spots read a status from there (getting whatever garbage is in that memory).

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:

  • treat an out-of-range ID as "already loaded" instead of reading the array,
  • skip the status write for an out-of-range ID,
  • skip the "still loading?" check for an out-of-range ID.

This mirrors the already-merged fix for the font status array (#6916), which was the same bug on the sound-font side.

Test plan

  • With a BGM pack that includes custom battle music, enter a dungeon and trigger an enemy encounter — battle music plays.
  • Back away until it stops, then re-approach — battle music plays again, reliably, on every subsequent encounter.
  • Repeat 5-10 times without a silent encounter.

Build Artifacts

@serprex
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
bassdr force-pushed the fix/saf-battle-music branch from 799e1bf to cec3eab Compare August 3, 2026 18:53
@serprex
serprex merged commit eafafe5 into HarbourMasters:develop Aug 3, 2026
5 checks passed
@bassdr
bassdr deleted the fix/saf-battle-music branch August 3, 2026 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Audio: Battle music playback issues

2 participants