diff --git a/UI/TextureUtil.cpp b/UI/TextureUtil.cpp index 959c3e85f5..6eb4a791f7 100644 --- a/UI/TextureUtil.cpp +++ b/UI/TextureUtil.cpp @@ -53,6 +53,13 @@ static bool LoadTextureLevels(const uint8_t *data, size_t size, ImageFileType ty if (1 == pngLoadPtr((const unsigned char *)data, size, &width[0], &height[0], &image[0], false)) { *num_levels = 1; *fmt = Draw::DataFormat::R8G8B8A8_UNORM; + if (!image[0]) { + ELOG("WTF"); + return false; + } + } else { + ELOG("PNG load failed"); + return false; } break; @@ -89,6 +96,10 @@ bool ManagedTexture::LoadFromFileData(const uint8_t *data, size_t dataSize, Imag return false; } + if (!image[0]) { + Crash(); + } + if (num_levels < 0 || num_levels >= 16) { ELOG("Invalid num_levels: %d. Falling back to one. Image: %dx%d", num_levels, width[0], height[0]); num_levels = 1; diff --git a/ext/native/image/png_load.cpp b/ext/native/image/png_load.cpp index 145d084ce3..41e90a88ec 100644 --- a/ext/native/image/png_load.cpp +++ b/ext/native/image/png_load.cpp @@ -84,14 +84,12 @@ int pngLoadPtr(const unsigned char *input_ptr, size_t input_len, int *pwidth, in #else if (flip) ELOG("pngLoad: flip flag not supported, image will be loaded upside down"); - png_image png; - memset(&png, 0, sizeof(png)); + png_image png{}; png.version = PNG_IMAGE_VERSION; png_image_begin_read_from_memory(&png, input_ptr, input_len); - if (PNG_IMAGE_FAILED(png)) - { + if (PNG_IMAGE_FAILED(png)) { ELOG("pngLoad: %s", png.message); return 0; } @@ -100,7 +98,14 @@ int pngLoadPtr(const unsigned char *input_ptr, size_t input_len, int *pwidth, in png.format = PNG_FORMAT_RGBA; int stride = PNG_IMAGE_ROW_STRIDE(png); - *image_data_ptr = (unsigned char *)malloc(PNG_IMAGE_SIZE(png)); + + size_t size = PNG_IMAGE_SIZE(png); + if (!size) { + ELOG("pngLoad: empty image"); + return 0; + } + + *image_data_ptr = (unsigned char *)malloc(size); png_image_finish_read(&png, NULL, *image_data_ptr, stride, NULL); #endif