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
master
az6667 2021-03-03 18:17:51 +11:00 committed by GitHub
parent a9a5b45983
commit fc38677e80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 2 deletions

View File

@ -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);

View File

@ -7,4 +7,10 @@ LIST(APPEND TESTS_FILES
bug00486
)
IF(PNG_FOUND)
LIST(APPEND TESTS_FILES
bug00486
)
ENDIF(PNG_FOUND)
ADD_GD_TESTS()

View File

@ -8,6 +8,7 @@
/bug00499
/gif_im2im
/gif_null
/gif_nocolormaps
/ossfuzz5700
/php_bug_75571
/uninitialized_memory_read

View File

@ -4,6 +4,7 @@ LIST(APPEND TESTS_FILES
bug00227
bug00499
gif_null
gif_nocolormaps
ossfuzz5700
php_bug_75571
uninitialized_memory_read

View File

@ -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

View File

@ -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();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 B