Skip to content
Permalink
Browse files Browse the repository at this point in the history
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 e6399f5 commit 10ef1dc
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/gd_tga.c
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/tga/.gitignore
@@ -1,3 +1,4 @@
/bug00084
/bug00247
/bug00247a
/tga_null
1 change: 1 addition & 0 deletions tests/tga/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ LIST(APPEND TESTS_FILES
tga_null
bug00084
bug00247
bug00247a
)

ADD_GD_TESTS()
4 changes: 3 additions & 1 deletion tests/tga/Makemodule.am
@@ -1,9 +1,11 @@
libgd_test_programs += \
tga/bug00084 \
tga/bug00247 \
tag/bug00247a \
tga/tga_null

EXTRA_DIST += \
tga/CMakeLists.txt \
tga/bug00084.tga \
tga/bug00247.tga
tga/bug00247.tga \
tga/bug00247a.tga
19 changes: 19 additions & 0 deletions tests/tga/bug00247a.c
@@ -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();
}
Binary file added tests/tga/bug00247a.tga
Binary file not shown.

0 comments on commit 10ef1dc

Please sign in to comment.