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 9da638f0c0
commit 72817df4cf
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");
@ -957,6 +958,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;