From a8192f47e2ae22d244835ecf433aaeec003d340c Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 30 Aug 2015 19:14:59 -0700 Subject: [PATCH] GBA: Better memory handling with PNG savestates --- CHANGES | 1 + src/gba/serialize.c | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index ff267b99e..0002e7c6d 100644 --- a/CHANGES +++ b/CHANGES @@ -14,6 +14,7 @@ Misc: - ARM7: Combine shifter-immediate and shifter-register functions to reduce binary size - SDL: Support fullscreen in SDL 1.2 - GBA: Attempting to save a screenshot-style savestate should be allowed without libpng + - GBA: Better memory handling with PNG savestates 0.3.0: (2015-08-16) Features: diff --git a/src/gba/serialize.c b/src/gba/serialize.c index 8acf95f97..bc772af90 100644 --- a/src/gba/serialize.c +++ b/src/gba/serialize.c @@ -234,12 +234,14 @@ static int _loadPNGChunkHandler(png_structp png, png_unknown_chunkp chunk) { if (strcmp((const char*) chunk->name, "gbAs") != 0) { return 0; } - struct GBASerializedState state; - uLongf len = sizeof(state); - uncompress((Bytef*) &state, &len, chunk->data, chunk->size); - if (!GBADeserialize(png_get_user_chunk_ptr(png), &state)) { + struct GBASerializedState* state = malloc(sizeof(*state)); + uLongf len = sizeof(*state); + uncompress((Bytef*) state, &len, chunk->data, chunk->size); + if (!GBADeserialize(png_get_user_chunk_ptr(png), state)) { + free(state); longjmp(png_jmpbuf(png), 1); } + free(state); return 1; } @@ -252,6 +254,10 @@ static bool _loadPNGState(struct GBA* gba, struct VFile* vf) { return false; } uint32_t* pixels = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4); + if (!pixels) { + PNGReadClose(png, info, end); + return false; + } PNGInstallChunkHandler(png, gba, _loadPNGChunkHandler, "gbAs"); bool success = PNGReadHeader(png, info);