From 55c2efa3ea6d9bb17b9a14344fa823779b093f7d Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 27 Sep 2022 05:32:13 -0700 Subject: [PATCH] GB: Don't try to map a 0-byte SRAM (fixes #2668) --- CHANGES | 1 + src/gb/gb.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 442380f5d..c374c18fc 100644 --- a/CHANGES +++ b/CHANGES @@ -63,6 +63,7 @@ Other fixes: - FFmpeg: Fix GIF recording (fixes mgba.io/i/2393) - GB: Fix temporary saves - GB: Fix replacing the ROM crashing when accessing ROM base + - GB: Don't try to map a 0-byte SRAM (fixes mgba.io/i/2668) - GB, GBA: Save writeback-pending masked saves on unload (fixes mgba.io/i/2396) - mGUI: Fix FPS counter after closing menu - Qt: Fix some hangs when using the debugger console diff --git a/src/gb/gb.c b/src/gb/gb.c index 3f9fd59a6..dc3a37191 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -276,13 +276,17 @@ void GBResizeSram(struct GB* gb, size_t size) { vf->seek(vf, size, SEEK_SET); vf->write(vf, extdataBuffer, vfSize & 0xFF); } - gb->memory.sram = vf->map(vf, size, MAP_WRITE); - memset(&gb->memory.sram[vfSize], 0xFF, size - vfSize); + if (size) { + gb->memory.sram = vf->map(vf, size, MAP_WRITE); + memset(&gb->memory.sram[vfSize], 0xFF, size - vfSize); + } } else if (size > gb->sramSize || !gb->memory.sram) { if (gb->memory.sram) { vf->unmap(vf, gb->memory.sram, gb->sramSize); } - gb->memory.sram = vf->map(vf, size, MAP_WRITE); + if (size) { + gb->memory.sram = vf->map(vf, size, MAP_WRITE); + } } } else { if (gb->memory.sram) { @@ -296,9 +300,11 @@ void GBResizeSram(struct GB* gb, size_t size) { gb->sramVf = newVf; vf->truncate(vf, size); } - gb->memory.sram = vf->map(vf, size, MAP_READ); + if (size) { + gb->memory.sram = vf->map(vf, size, MAP_READ); + } } - if (gb->memory.sram == (void*) -1) { + if (!size || gb->memory.sram == (void*) -1) { gb->memory.sram = NULL; } } else if (size) {