From fc38677e8005c24db891b84d3e84df7a199c979c Mon Sep 17 00:00:00 2001 From: az6667 Date: Wed, 3 Mar 2021 18:17:51 +1100 Subject: [PATCH] Allow GIFs missing both Global and Local Colormaps to still decode (#494) Ensure that a GIF without any Global or Local color tables is still decoded by libgd. GIF89a spec indicates conforming image files need not have Global or Local color tables at all. Spec recommends creating custom color map in that situation, and that at least Black+White as first two entries, to ensure B&W images are decoded. Some commonly used single-pixel GIFs found around the web are undecoded by libgd otherwise. Test case has been included. References: https://www.w3.org/Graphics/GIF/spec-gif89a.txt http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever --- src/gd_gif_in.c | 6 ++++-- tests/gdimagecrop/CMakeLists.txt | 6 ++++++ tests/gif/.gitignore | 1 + tests/gif/CMakeLists.txt | 1 + tests/gif/Makemodule.am | 2 ++ tests/gif/gif_nocolormaps.c | 25 +++++++++++++++++++++++++ tests/gif/gif_nocolormaps.gif | Bin 0 -> 37 bytes 7 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 tests/gif/gif_nocolormaps.c create mode 100644 tests/gif/gif_nocolormaps.gif diff --git a/src/gd_gif_in.c b/src/gd_gif_in.c index 1ae0e32..2b6b34e 100644 --- a/src/gd_gif_in.c +++ b/src/gd_gif_in.c @@ -315,8 +315,10 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromGifCtx(gdIOCtxPtr fd) ReadImage(im, fd, width, height, localColorMap, BitSet(buf[8], INTERLACE), &ZeroDataBlock); } else { if(!haveGlobalColormap) { - gdImageDestroy(im); - return 0; + // Still a valid gif, apply simple default palette as per spec + ColorMap[CM_RED][1] = 0xff; + ColorMap[CM_GREEN][1] = 0xff; + ColorMap[CM_BLUE][1] = 0xff; } ReadImage(im, fd, width, height, ColorMap, BitSet(buf[8], INTERLACE), &ZeroDataBlock); diff --git a/tests/gdimagecrop/CMakeLists.txt b/tests/gdimagecrop/CMakeLists.txt index 43f6f6c..f5c309e 100644 --- a/tests/gdimagecrop/CMakeLists.txt +++ b/tests/gdimagecrop/CMakeLists.txt @@ -7,4 +7,10 @@ LIST(APPEND TESTS_FILES bug00486 ) +IF(PNG_FOUND) +LIST(APPEND TESTS_FILES + bug00486 +) +ENDIF(PNG_FOUND) + ADD_GD_TESTS() diff --git a/tests/gif/.gitignore b/tests/gif/.gitignore index 1b07664..d30118f 100644 --- a/tests/gif/.gitignore +++ b/tests/gif/.gitignore @@ -8,6 +8,7 @@ /bug00499 /gif_im2im /gif_null +/gif_nocolormaps /ossfuzz5700 /php_bug_75571 /uninitialized_memory_read diff --git a/tests/gif/CMakeLists.txt b/tests/gif/CMakeLists.txt index c4b9007..d071874 100644 --- a/tests/gif/CMakeLists.txt +++ b/tests/gif/CMakeLists.txt @@ -4,6 +4,7 @@ LIST(APPEND TESTS_FILES bug00227 bug00499 gif_null + gif_nocolormaps ossfuzz5700 php_bug_75571 uninitialized_memory_read diff --git a/tests/gif/Makemodule.am b/tests/gif/Makemodule.am index 9306b71..c616b3e 100644 --- a/tests/gif/Makemodule.am +++ b/tests/gif/Makemodule.am @@ -4,6 +4,7 @@ libgd_test_programs += \ gif/bug00227 \ gif/bug00499 \ gif/gif_null \ + gif/gif_nocolormaps \ gif/ossfuzz5700 \ gif/php_bug_75571 \ gif/uninitialized_memory_read \ @@ -27,6 +28,7 @@ EXTRA_DIST += \ gif/bug00060.gif \ gif/bug00066.gif \ gif/bug00066_exp.png \ + gif/gif_nocolormaps.gif \ gif/ossfuzz5700.gif \ gif/php_bug_75571.gif \ gif/unitialized_memory_read.gif diff --git a/tests/gif/gif_nocolormaps.c b/tests/gif/gif_nocolormaps.c new file mode 100644 index 0000000..4419904 --- /dev/null +++ b/tests/gif/gif_nocolormaps.c @@ -0,0 +1,25 @@ +/** + * Ensure that a GIF without any Global or Local color tables is still decoded + * + * GIF89a spec indicates conforming image files need not have Global or Local color tables at all + * + * See also: https://www.w3.org/Graphics/GIF/spec-gif89a.txt + * http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever + */ + +#include "gd.h" +#include "gdtest.h" + +int main() +{ + gdImagePtr im; + FILE *fp; + + fp = gdTestFileOpen2("gif", "gif_nocolormaps.gif"); + gdTestAssert(fp != NULL); + im = gdImageCreateFromGif(fp); + gdTestAssert(im != NULL); + fclose(fp); + + return gdNumFailures(); +} diff --git a/tests/gif/gif_nocolormaps.gif b/tests/gif/gif_nocolormaps.gif new file mode 100644 index 0000000000000000000000000000000000000000..b3aa80d843a929ce4e5af18912072ba4157c115b GIT binary patch literal 37 kcmZ?wbhEHbWMp7u00PCIER0-0rVfY$7Gq-aVPvod09bhgj{pDw literal 0 HcmV?d00001