diff --git a/src/gba/cart/ereader.c b/src/gba/cart/ereader.c index 5edfe193e..9da25196c 100644 --- a/src/gba/cart/ereader.c +++ b/src/gba/cart/ereader.c @@ -888,7 +888,11 @@ struct EReaderScan* EReaderScanLoadImagePNG(const char* filename) { } png_infop info = png_create_info_struct(png); png_infop end = png_create_info_struct(png); - PNGReadHeader(png, info); + if (!PNGReadHeader(png, info)) { + PNGReadClose(png, info, end); + vf->close(vf); + return NULL; + } unsigned height = png_get_image_height(png, info); unsigned width = png_get_image_width(png, info); int type = png_get_color_type(png, info); @@ -900,19 +904,34 @@ struct EReaderScan* EReaderScanLoadImagePNG(const char* filename) { break; } image = malloc(height * width * 3); - PNGReadPixels(png, info, image, width, height, width); + if (!image) { + goto out; + } + if (!PNGReadPixels(png, info, image, width, height, width)) { + free(image); + image = NULL; + goto out; + } break; case PNG_COLOR_TYPE_RGBA: if (depth != 8) { break; } image = malloc(height * width * 4); - PNGReadPixelsA(png, info, image, width, height, width); + if (!image) { + goto out; + } + if (!PNGReadPixelsA(png, info, image, width, height, width)) { + free(image); + image = NULL; + goto out; + } break; default: break; } PNGReadFooter(png, end); +out: PNGReadClose(png, info, end); vf->close(vf); if (!image) {