Skip to content
Permalink
Browse files

Unsupported TGA bpp/alphabit combinations should error gracefully

Currently, only 24bpp without alphabits and 32bpp with 8 alphabits are
really supported. All other combinations will be rejected with a warning.
  • Loading branch information...
cmb69 committed Jul 12, 2016
1 parent 076e0d1 commit cb1a0b7e54e9aa118270c23a4a6fe560e4590dc9
Showing with 30 additions and 11 deletions.
  1. +6 −10 src/gd_tga.c
  2. +1 −0 tests/tga/.gitignore
  3. +1 −0 tests/tga/CMakeLists.txt
  4. +3 −1 tests/tga/Makemodule.am
  5. +19 −0 tests/tga/bug00247a.c
  6. BIN tests/tga/bug00247a.tga
@@ -99,7 +99,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtx* ctx)
if (tga->bits == TGA_BPP_24) {
*tpix = gdTrueColor(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret]);
bitmap_caret += 3;
} else if (tga->bits == TGA_BPP_32 || tga->alphabits) {
} else if (tga->bits == TGA_BPP_32 && tga->alphabits) {
register int a = tga->bitmap[bitmap_caret + 3];

*tpix = gdTrueColorAlpha(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret], gdAlphaMax - (a >> 1));
@@ -159,16 +159,12 @@ int read_header_tga(gdIOCtx *ctx, oTga *tga)
printf("wxh: %i %i\n", tga->width, tga->height);
#endif

switch(tga->bits) {
case 8:
case 16:
case 24:
case 32:
break;
default:
gd_error("bps %i not supported", tga->bits);
if (!((tga->bits == TGA_BPP_24 && tga->alphabits == 0)
|| (tga->bits == TGA_BPP_32 && tga->alphabits == 8)))
{
gd_error_ex(GD_WARNING, "gd-tga: %u bits per pixel with %u alpha bits not supported\n",
tga->bits, tga->alphabits);
return -1;
break;
}

tga->ident = NULL;
@@ -1,3 +1,4 @@
/bug00084
/bug00247
/bug00247a
/tga_null
@@ -2,6 +2,7 @@ LIST(APPEND TESTS_FILES
tga_null
bug00084
bug00247
bug00247a
)

ADD_GD_TESTS()
@@ -1,9 +1,11 @@
libgd_test_programs += \
tga/bug00084 \
tga/bug00247 \
tga/bug00247a \
tga/tga_null

EXTRA_DIST += \
tga/CMakeLists.txt \
tga/bug00084.tga \
tga/bug00247.tga
tga/bug00247.tga \
tga/bug00247a.tga
@@ -0,0 +1,19 @@
/*
We test that a 8bpp TGA file will be gracefully rejected by
gdImageCreateFromTga().
*/

#include <stdio.h>

#include "gd.h"
#include "gdtest.h"

int main(int argc, char **argv)
{
gdImagePtr im;
FILE *fp = gdTestFileOpen("tga/bug00247a.tga");
im = gdImageCreateFromTga(fp);
gdTestAssert(im == NULL);
fclose(fp);
return gdNumFailures();
}
BIN +36 Bytes tests/tga/bug00247a.tga
Binary file not shown.

0 comments on commit cb1a0b7

Please sign in to comment.
You can’t perform that action at this time.