From a5bef44d658322698b3a7de9809e6f0215d1b2b5 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 10 Oct 2018 14:57:27 -0700 Subject: [PATCH] Improve handling oof corrupted PNG images (resolves openfl/openfl#1999) --- project/src/graphics/format/PNG.cpp | 10 +++++++--- src/lime/graphics/Image.hx | 31 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/project/src/graphics/format/PNG.cpp b/project/src/graphics/format/PNG.cpp index d56845a21..1f3a3a0c0 100644 --- a/project/src/graphics/format/PNG.cpp +++ b/project/src/graphics/format/PNG.cpp @@ -21,9 +21,9 @@ namespace lime { ReadBuffer (const unsigned char* data, int length) : data (data), length (length), position (0) {} - void Read (unsigned char* out, int count) { + bool Read (unsigned char* out, int count) { - if (position >= length) return; + if (position >= length) return false; if (count > length - position) { @@ -37,6 +37,8 @@ namespace lime { } + return true; + } char unused; // the first byte gets corrupted when passed to libpng? @@ -57,7 +59,9 @@ namespace lime { static void user_read_data_fn (png_structp png_ptr, png_bytep data, png_size_t length) { ReadBuffer* buffer = (ReadBuffer*)png_get_io_ptr (png_ptr); - buffer->Read (data, length); + if (!buffer->Read (data, length)) { + png_error (png_ptr, "Read Error"); + } } diff --git a/src/lime/graphics/Image.hx b/src/lime/graphics/Image.hx index 1260e7e76..ac2056cba 100644 --- a/src/lime/graphics/Image.hx +++ b/src/lime/graphics/Image.hx @@ -724,8 +724,11 @@ class Image { if (bytes == null) return null; var image = new Image (); - image.__fromBytes (bytes); - return image; + if (image.__fromBytes (bytes)) { + return image; + } else { + return null; + } } @@ -768,8 +771,11 @@ class Image { if (path == null) return null; var image = new Image (); - image.__fromFile (path); - return image; + if (image.__fromFile (path)) { + return image; + } else { + return null; + } } @@ -1694,7 +1700,7 @@ class Image { } - @:noCompletion private function __fromBytes (bytes:Bytes, onload:Image->Void = null):Void { + @:noCompletion private function __fromBytes (bytes:Bytes, onload:Image->Void = null):Bool { #if (js && html5) @@ -1720,6 +1726,7 @@ class Image { } __fromBase64 (__base64Encode (bytes), type, onload); + return true; #elseif (lime_cffi && !macro) @@ -1744,6 +1751,8 @@ class Image { } + return true; + } #else @@ -1752,10 +1761,12 @@ class Image { #end + return false; + } - @:noCompletion private function __fromFile (path:String, onload:Image->Void = null, onerror:Void->Void = null):Void { + @:noCompletion private function __fromFile (path:String, onload:Image->Void = null, onerror:Void->Void = null):Bool { #if (kha && !macro) @@ -1798,6 +1809,8 @@ class Image { } + return true; + } } catch (e:Dynamic) {} @@ -1852,6 +1865,8 @@ class Image { // (issue #1019768) if (image.complete) { } + return true; + #elseif (lime_cffi || java) var buffer:ImageBuffer = null; @@ -1921,6 +1936,8 @@ class Image { } + return true; + } #else @@ -1929,6 +1946,8 @@ class Image { #end + return false; + }