GBA: Fix crash when new size is larger than rom size after soft-patching

This commit is contained in:
negativeExponent 2020-01-04 00:21:29 +08:00
parent 9e1a63af0b
commit b50d48458f
3 changed files with 23 additions and 1 deletions

View File

@ -458,6 +458,23 @@ variable_desc saveGameStruct[] = {
static int romSize = SIZE_ROM;
void gbaUpdateRomSize(int size)
{
// Only change memory block if new size is larger
if (size > romSize) {
romSize = size;
uint8_t* tmp = (uint8_t*)realloc(rom, SIZE_ROM);
rom = tmp;
uint16_t* temp = (uint16_t*)(rom + ((romSize + 1) & ~1));
for (int i = (romSize + 1) & ~1; i < SIZE_ROM; i += 2) {
WRITE16LE(temp, (i >> 1) & 0xFFFF);
temp++;
}
}
}
#ifdef PROFILING
void cpuProfil(profile_segment* seg)
{
@ -1506,7 +1523,7 @@ int CPULoadRom(const char* szFile)
uint16_t* temp = (uint16_t*)(rom + ((romSize + 1) & ~1));
int i;
for (i = (romSize + 1) & ~1; i < romSize; i += 2) {
for (i = (romSize + 1) & ~1; i < SIZE_ROM; i += 2) {
WRITE16LE(temp, (i >> 1) & 0xFFFF);
temp++;
}

View File

@ -165,6 +165,9 @@ const char* GetSaveDotCodeFile();
void SetLoadDotCodeFile(const char* szFile);
void SetSaveDotCodeFile(const char* szFile);
// Updates romSize and realloc rom pointer if needed after soft-patching
void gbaUpdateRomSize(int size);
extern struct EmulatedSystem GBASystem;
#define R13_IRQ 18

View File

@ -223,6 +223,8 @@ void GameArea::LoadGame(const wxString& name)
int size = 0x2000000 < rom_size ? 0x2000000 : rom_size;
applyPatch(pfn.GetFullPath().mb_str(), &rom, &size);
// that means we no longer really know rom_size either <sigh>
gbaUpdateRomSize(size);
}
wxFileConfig* cfg = wxGetApp().overrides;