Skip to content

Commit 6d3fa65

Browse files
committed
prevent invalid color index (palette only), may lead to crash
1 parent 62f9d1e commit 6d3fa65

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

ext/gd/libgd/gd.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,12 @@ void gdImageFillToBorder (gdImagePtr im, int x, int y, int border, int color)
17691769
return;
17701770
}
17711771

1772+
if (!im->trueColor) {
1773+
if ((color > (im->colorsTotal - 1)) || (border > (im->colorsTotal - 1)) || (color < 0)) {
1774+
return;
1775+
}
1776+
}
1777+
17721778
restoreAlphaBlending = im->alphaBlendingFlag;
17731779
im->alphaBlendingFlag = 0;
17741780

ext/gd/tests/github_bug_215.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Github #215 (imagefilltoborder stack overflow when invalid pallete index used)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("gd")) die("skip GD not present");
6+
?>
7+
--FILE--
8+
<?php
9+
$image = imagecreate( 10, 10 );
10+
$bgd = imagecolorallocate( $image, 0, 0, 0 );
11+
$border = imagecolorallocate( $image, 255, 0, 0 );
12+
$fillcolor = imagecolorallocate( $image, 255, 0, 0 );
13+
14+
/* Use unallocated color index */
15+
imagefilltoborder( $image, 0,0, $border+10, $fillcolor);
16+
echo "#1 passes\n";
17+
18+
/* Use negative color index */
19+
imagefilltoborder( $image, 0,0, -$border, $fillcolor);
20+
echo "#2 passes\n";
21+
22+
23+
/* Use unallocated color index */
24+
imagefilltoborder( $image, 0,0, $border, $fillcolor+10);
25+
echo "#3 passes\n";
26+
27+
/* Use negative color index */
28+
imagefilltoborder( $image, 0,0, $border, -$fillcolor);
29+
echo "#4 passes\n";
30+
31+
32+
/* Use negative color index */
33+
imagefilltoborder( $image, 0,0, $border+10, $fillcolor+10);
34+
echo "#5 passes";
35+
36+
37+
?>
38+
--EXPECT--
39+
#1 passes
40+
#2 passes
41+
#3 passes
42+
#4 passes
43+
#5 passes

0 commit comments

Comments
 (0)