all repos — mgba @ d37cf5f0c3f7af64fee221317d1a05a482a07184

mGBA Game Boy Advance Emulator

Qt: Refactor PNG savestate code for slightly better behavior
Jeffrey Pfau jeffrey@endrift.com
Sat, 02 May 2015 01:13:56 -0700
commit

d37cf5f0c3f7af64fee221317d1a05a482a07184

parent

07a3d08b26f7c26063ebd9a372aaacc40c2542f2

1 files changed, 17 insertions(+), 6 deletions(-)

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

@@ -135,20 +135,31 @@ return false;

} struct GBASerializedState* state = GBAAllocateState(); + if (!state) { + return false; + } png_structp png = PNGWriteOpen(vf); png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS); + if (!png || !info) { + PNGWriteClose(png, info); + GBADeallocateState(state); + return false; + } uLongf len = compressBound(sizeof(*state)); void* buffer = malloc(len); - if (state && png && info && buffer) { - GBASerialize(gba, state); - compress(buffer, &len, (const Bytef*) state, sizeof(*state)); - PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels); - PNGWriteCustomChunk(png, "gbAs", len, buffer); + if (!buffer) { + PNGWriteClose(png, info); + GBADeallocateState(state); + return false; } + GBASerialize(gba, state); + compress(buffer, &len, (const Bytef*) state, sizeof(*state)); + PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels); + PNGWriteCustomChunk(png, "gbAs", len, buffer); PNGWriteClose(png, info); free(buffer); GBADeallocateState(state); - return state && png && info && buffer; + return true; } static int _loadPNGChunkHandler(png_structp png, png_unknown_chunkp chunk) {