GB: Fix potential double-free of non-pristine ROM memory

This commit is contained in:
Vicki Pfau 2023-03-01 20:11:26 -08:00
parent e3983d3330
commit 064d6ce183
2 changed files with 9 additions and 1 deletions

View File

@ -405,7 +405,9 @@ void GBUnloadROM(struct GB* gb) {
if (gb->romVf) {
#ifndef FIXED_ROM_BUFFER
gb->romVf->unmap(gb->romVf, gb->memory.rom, gb->pristineRomSize);
if (gb->isPristine && gb->memory.rom) {
gb->romVf->unmap(gb->romVf, gb->memory.rom, gb->pristineRomSize);
}
#endif
gb->romVf->close(gb->romVf);
gb->romVf = NULL;

View File

@ -14,6 +14,7 @@
#include <mgba/internal/sm83/sm83.h>
#include <mgba-util/memory.h>
#include <mgba-util/vfs.h>
mLOG_DEFINE_CATEGORY(GB_MEM, "GB Memory", "gb.memory");
@ -1006,6 +1007,11 @@ void _pristineCow(struct GB* gb) {
if (gb->memory.rom == gb->memory.romBase) {
gb->memory.romBase = newRom;
}
if (gb->romVf) {
gb->romVf->unmap(gb->romVf, gb->memory.rom, gb->memory.romSize);
gb->romVf->close(gb->romVf);
gb->romVf = NULL;
}
gb->memory.rom = newRom;
GBMBCSwitchBank(gb, gb->memory.currentBank);
gb->isPristine = false;