Skip to content

Commit 8d6e958

Browse files
cmb69smalyshev
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.
1 parent 73ca9b3 commit 8d6e958

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
@@ -261,10 +261,6 @@ gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr fd) /* {{{ */
261261
if (!im) {
262262
return 0;
263263
}
264-
if (!im->colorsTotal) {
265-
gdImageDestroy(im);
266-
return 0;
267-
}
268264
/* Check for open colors at the end, so
269265
we can reduce colorsTotal and ultimately
270266
BitsPerPixel */
@@ -275,6 +271,10 @@ gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr fd) /* {{{ */
275271
break;
276272
}
277273
}
274+
if (!im->colorsTotal) {
275+
gdImageDestroy(im);
276+
return 0;
277+
}
278278
return im;
279279
}
280280
/* }}} */
@@ -375,7 +375,7 @@ static int
375375
GetCode_(gdIOCtx *fd, CODE_STATIC_DATA *scd, int code_size, int flag, int *ZeroDataBlockP)
376376
{
377377
int i, j, ret;
378-
unsigned char count;
378+
int count;
379379

380380
if (flag) {
381381
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)