all repos — mgba @ 9262475cb47c1bbfd4422591394b789ce18f6b6b

mGBA Game Boy Advance Emulator

GBA Savedata: Fix various filesize edge cases (fixes #1221)
Vicki Pfau vi@endrift.com
Fri, 09 Nov 2018 20:31:30 -0800
commit

9262475cb47c1bbfd4422591394b789ce18f6b6b

parent

32279c37b1512f579208ee6d700045ad6031f6c7

2 files changed, 10 insertions(+), 11 deletions(-)

jump to
M src/gba/savedata.csrc/gba/savedata.c

@@ -259,9 +259,6 @@ } else {

end = savedata->vf->size(savedata->vf); if (end < flashSize) { savedata->vf->truncate(savedata->vf, flashSize); - } else if (end >= SIZE_CART_FLASH1M) { - flashSize = SIZE_CART_FLASH1M; - savedata->type = SAVEDATA_FLASH1M; } savedata->data = savedata->vf->map(savedata->vf, flashSize, savedata->mapMode); }

@@ -291,9 +288,6 @@ } else {

end = savedata->vf->size(savedata->vf); if (end < eepromSize) { savedata->vf->truncate(savedata->vf, eepromSize); - } else if (end >= SIZE_CART_EEPROM) { - eepromSize = SIZE_CART_EEPROM; - savedata->type = SAVEDATA_EEPROM; } savedata->data = savedata->vf->map(savedata->vf, eepromSize, savedata->mapMode); }

@@ -439,13 +433,17 @@ if (savedata->type == SAVEDATA_EEPROM) {

return; } savedata->type = SAVEDATA_EEPROM; - if (!savedata->vf || savedata->vf->size(savedata->vf) > SIZE_CART_EEPROM512) { + if (!savedata->vf) { return; } savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_EEPROM512); - savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM); - savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode); - memset(&savedata->data[SIZE_CART_EEPROM512], 0xFF, SIZE_CART_EEPROM - SIZE_CART_EEPROM512); + if (savedata->vf->size(savedata->vf) < SIZE_CART_EEPROM) { + savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM); + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode); + memset(&savedata->data[SIZE_CART_EEPROM512], 0xFF, SIZE_CART_EEPROM - SIZE_CART_EEPROM512); + } else { + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode); + } } void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize) {

@@ -605,7 +603,7 @@ mLOG(GBA_SAVE, INFO, "Updating flash chip from 512kb to 1Mb");

savedata->type = SAVEDATA_FLASH1M; if (savedata->vf) { savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_FLASH512); - if (savedata->vf->size(savedata->vf) == SIZE_CART_FLASH512) { + if (savedata->vf->size(savedata->vf) < SIZE_CART_FLASH1M) { savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M); savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE); memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512);
M src/platform/libretro/libretro.csrc/platform/libretro/libretro.c

@@ -489,6 +489,7 @@

core->setPeripheral(core, mPERIPH_RUMBLE, &rumble); savedata = anonymousMemoryMap(SIZE_CART_FLASH1M); + memset(savedata, 0xFF, SIZE_CART_FLASH1M); struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M); _reloadSettings();