Skip to content

Commit 3b50e23

Browse files
cmb69sgolemon
authored andcommitted
Fixed bug #75571: Potential infinite loop in gdImageCreateFromGifCtx
Due to a signedness confusion in `GetCode_` a corrupt GIF file can trigger an infinite loop. Furthermore we make sure that a GIF without any palette entries is treated as invalid *after* open palette entries have been removed. (cherry picked from commit 8d6e958)
1 parent 8c26020 commit 3b50e23

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

ext/gd/libgd/gd_gif_in.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,6 @@ gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr fd) /* {{{ */
262262
if (!im) {
263263
return 0;
264264
}
265-
if (!im->colorsTotal) {
266-
gdImageDestroy(im);
267-
return 0;
268-
}
269265
/* Check for open colors at the end, so
270266
we can reduce colorsTotal and ultimately
271267
BitsPerPixel */
@@ -276,6 +272,10 @@ gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr fd) /* {{{ */
276272
break;
277273
}
278274
}
275+
if (!im->colorsTotal) {
276+
gdImageDestroy(im);
277+
return 0;
278+
}
279279
return im;
280280
}
281281
/* }}} */
@@ -376,7 +376,7 @@ static int
376376
GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
377377
{
378378
int i, j, ret;
379-
unsigned char count;
379+
int count;
380380

381381
if (flag) {
382382
scd->curbit = 0;

ext/gd/tests/bug75571.gif

1.69 KB
Loading

ext/gd/tests/bug75571.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Bug #75571 (Infinite loop in GIF reading causing DoS)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(imagecreatefromgif(__DIR__ . '/bug75571.gif'));
10+
?>
11+
===DONE===
12+
--EXPECTF--
13+
Warning: imagecreatefromgif(): '%s' is not a valid GIF file in %s on line %d
14+
bool(false)
15+
===DONE===

0 commit comments

Comments
 (0)