From a6429d729b4ffb7939ac73fd9c8f6c46e4760131 Mon Sep 17 00:00:00 2001 From: Sonic Dreamcaster Date: Sun, 4 Jan 2026 08:10:28 -0300 Subject: [PATCH] audio UB fix correction --- src/audio/audio_heap.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/audio/audio_heap.c b/src/audio/audio_heap.c index b1e0cb49..d8207e8a 100644 --- a/src/audio/audio_heap.c +++ b/src/audio/audio_heap.c @@ -994,15 +994,25 @@ void AudioHeap_DiscardSampleCaches(void) { for (fontId = 0; fontId < numFonts; fontId++) { sampleBankId1 = gSoundFontList[fontId].sampleBankId1; sampleBankId2 = gSoundFontList[fontId].sampleBankId2; - // @port: avoid reading from garbage memory, +#ifdef AVOID_UB // F-Zero X newer version of this audio driver has this fix: - if (((sampleBankId1 != SAMPLES_NONE_U) /* && (entry->sampleBankId == sampleBankId1) */) || - ((sampleBankId2 != SAMPLES_NONE) /* && (entry->sampleBankId == sampleBankId2)*/ ) /* || - (entry->sampleBankId == SAMPLES_SFX) */) { + if ((sampleBankId1 != SAMPLES_NONE_U) || (sampleBankId2 != SAMPLES_NONE)) { +#else + //! @bug: entry is uninitialized, reading from garbage memory. + if (((sampleBankId1 != SAMPLES_NONE_U) && (entry->sampleBankId == sampleBankId1)) || + ((sampleBankId2 != SAMPLES_NONE) && (entry->sampleBankId == sampleBankId2)) || + (entry->sampleBankId == SAMPLES_SFX)) { +#endif if (((void*) AudioHeap_SearchCaches(FONT_TABLE, CACHE_PERMANENT, fontId) != NULL) && ((gFontLoadStatus[fontId] > 1) != 0)) { for (i = 0; i < gPersistentSampleCache.numEntries; i++) { entry = &gPersistentSampleCache.entries[i]; +#ifdef AVOID_UB + if ((sampleBankId1 != entry->sampleBankId) && (sampleBankId2 != entry->sampleBankId) && + (entry->sampleBankId != SAMPLES_SFX)) { + break; + } +#endif for (instId = 0; instId < gSoundFontList[fontId].numInstruments; instId++) { instrument = Audio_GetInstrument(fontId, instId); if (instrument != NULL) {