GBA Savedata: Fix crash when resizing flash save games for RTC data

This commit is contained in:
Vicki Pfau 2023-11-22 03:57:13 -08:00
parent 050c5da263
commit ce4024a29e
2 changed files with 10 additions and 0 deletions

View File

@ -26,6 +26,7 @@ Emulation fixes:
Other fixes:
- Core: Fix inconsistencies with setting game-specific overrides (fixes mgba.io/i/2963)
- Debugger: Fix writing to specific segment in command-line debugger
- GBA Savedata: Fix crash when resizing flash save games for RTC data
- mGUI: Fix cases where an older save state screenshot would be shown. (fixes mgba.io/i/2183)
- Qt: Fix savestate preview sizes with different scales (fixes mgba.io/i/2560)
- Qt: Re-enable sync for multiplayer windows that aren't connected (fixes mgba.io/i/2974)

View File

@ -602,14 +602,23 @@ void GBASavedataRTCWrite(struct GBASavedata* savedata) {
size_t size = GBASavedataSize(savedata);
savedata->vf->seek(savedata->vf, size & ~0xFF, SEEK_SET);
int bank = 0;
if ((savedata->vf->size(savedata->vf) & 0xFF) != sizeof(buffer)) {
// Writing past the end of the file can invalidate the file mapping
if (savedata->type == SAVEDATA_FLASH1M) {
bank = savedata->currentBank == &savedata->data[0x10000];
}
savedata->vf->unmap(savedata->vf, savedata->data, size);
savedata->data = NULL;
}
savedata->vf->write(savedata->vf, &buffer, sizeof(buffer));
if (!savedata->data) {
savedata->data = savedata->vf->map(savedata->vf, size, MAP_WRITE);
if (savedata->type == SAVEDATA_FLASH1M) {
savedata->currentBank = &savedata->data[bank << 16];
} else if (savedata->type == SAVEDATA_FLASH512) {
savedata->currentBank = savedata->data;
}
}
}