GB Memory: Fix potential crash when directly accessing invalid SRAM

This commit is contained in:
Vicki Pfau 2023-03-01 16:13:35 -08:00
parent e504ac3665
commit 622a6e9e2d
1 changed files with 7 additions and 6 deletions

View File

@ -474,13 +474,14 @@ uint8_t GBView8(struct SM83Core* cpu, uint16_t address, int segment) {
if (memory->rtcAccess) { if (memory->rtcAccess) {
return memory->rtcRegs[memory->activeRtcReg]; return memory->rtcRegs[memory->activeRtcReg];
} else if (memory->sramAccess) { } else if (memory->sramAccess) {
if (segment < 0 && memory->sram) { if (memory->sram) {
return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)]; if (segment < 0) {
} else if ((size_t) segment * GB_SIZE_EXTERNAL_RAM < gb->sramSize) { return memory->sramBank[address & (GB_SIZE_EXTERNAL_RAM - 1)];
return memory->sram[(address & (GB_SIZE_EXTERNAL_RAM - 1)) + segment *GB_SIZE_EXTERNAL_RAM]; } else if ((size_t) segment * GB_SIZE_EXTERNAL_RAM < gb->sramSize) {
} else { return memory->sram[(address & (GB_SIZE_EXTERNAL_RAM - 1)) + segment *GB_SIZE_EXTERNAL_RAM];
return 0xFF; }
} }
return 0xFF;
} else if (memory->mbcRead) { } else if (memory->mbcRead) {
return memory->mbcRead(memory, address); return memory->mbcRead(memory, address);
} else if (memory->mbcType == GB_HuC3) { } else if (memory->mbcType == GB_HuC3) {