Skip to content

Commit 10ef1dc

Browse files
committed
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.
1 parent e6399f5 commit 10ef1dc

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

Diff for: src/gd_tga.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtx* ctx)
9999
if (tga->bits == TGA_BPP_24) {
100100
*tpix = gdTrueColor(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret]);
101101
bitmap_caret += 3;
102-
} else if (tga->bits == TGA_BPP_32 || tga->alphabits) {
102+
} else if (tga->bits == TGA_BPP_32 && tga->alphabits) {
103103
register int a = tga->bitmap[bitmap_caret + 3];
104104

105105
*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)
159159
printf("wxh: %i %i\n", tga->width, tga->height);
160160
#endif
161161

162-
switch(tga->bits) {
163-
case 8:
164-
case 16:
165-
case 24:
166-
case 32:
167-
break;
168-
default:
169-
gd_error("bps %i not supported", tga->bits);
162+
if (!((tga->bits == TGA_BPP_24 && tga->alphabits == 0)
163+
|| (tga->bits == TGA_BPP_32 && tga->alphabits == 8)))
164+
{
165+
gd_error_ex(GD_WARNING, "gd-tga: %u bits per pixel with %u alpha bits not supported\n",
166+
tga->bits, tga->alphabits);
170167
return -1;
171-
break;
172168
}
173169

174170
tga->ident = NULL;

Diff for: tests/tga/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/bug00084
22
/bug00247
3+
/bug00247a
34
/tga_null

Diff for: tests/tga/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ LIST(APPEND TESTS_FILES
22
tga_null
33
bug00084
44
bug00247
5+
bug00247a
56
)
67

78
ADD_GD_TESTS()

Diff for: tests/tga/Makemodule.am

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
libgd_test_programs += \
22
tga/bug00084 \
33
tga/bug00247 \
4+
tag/bug00247a \
45
tga/tga_null
56

67
EXTRA_DIST += \
78
tga/CMakeLists.txt \
89
tga/bug00084.tga \
9-
tga/bug00247.tga
10+
tga/bug00247.tga \
11+
tga/bug00247a.tga

Diff for: tests/tga/bug00247a.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
We test that a 8bpp TGA file will be gracefully rejected by
3+
gdImageCreateFromTga().
4+
*/
5+
6+
#include <stdio.h>
7+
8+
#include "gd.h"
9+
#include "gdtest.h"
10+
11+
int main(int argc, char **argv)
12+
{
13+
gdImagePtr im;
14+
FILE *fp = gdTestFileOpen("tga/bug00247a.tga");
15+
im = gdImageCreateFromTga(fp);
16+
gdTestAssert(im == NULL);
17+
fclose(fp);
18+
return gdNumFailures();
19+
}

Diff for: tests/tga/bug00247a.tga

36 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)