Skip to content

Commit

Permalink
GBA: Fix crash when new size is larger than rom size after soft-patching
Browse files Browse the repository at this point in the history
  • Loading branch information
negativeExponent committed Jan 3, 2020
1 parent 9e1a63a commit b50d484
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/gba/GBA.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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++;
}
Expand Down
3 changes: 3 additions & 0 deletions src/gba/GBA.h
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/wx/panel.cpp
Expand Up @@ -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;
Expand Down

0 comments on commit b50d484

Please sign in to comment.