Skip to content

Commit 567c9f5

Browse files
cmb69smalyshev
authored andcommitted
Fix #77270: imagecolormatch Out Of Bounds Write on Heap
At least some of the image reading functions may return images which use color indexes greater than or equal to im->colorsTotal. We cater to this by always using a buffer size which is sufficient for `gdMaxColors` in `gdImageColorMatch()`.
1 parent 4feb9e6 commit 567c9f5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

ext/gd/libgd/gd_color.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ int gdImageColorMatch (gdImagePtr im1, gdImagePtr im2)
3333
return -4; /* At least 1 color must be allocated */
3434
}
3535

36-
buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * im2->colorsTotal, 0);
37-
memset( buf, 0, sizeof(unsigned long) * 5 * im2->colorsTotal );
36+
buf = (unsigned long *)safe_emalloc(sizeof(unsigned long), 5 * gdMaxColors, 0);
37+
memset( buf, 0, sizeof(unsigned long) * 5 * gdMaxColors );
3838

3939
for (x=0; x<im1->sx; x++) {
4040
for( y=0; y<im1->sy; y++ ) {

ext/gd/tests/bug77270.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Bug #77270 (imagecolormatch Out Of Bounds Write on Heap)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('gd')) die('skip gd extension not available');
6+
if (!GD_BUNDLED && version_compare(GD_VERSION, '2.2.5', '<=')) die('skip upstream bugfix has not been released');
7+
?>
8+
--FILE--
9+
<?php
10+
$img1 = imagecreatetruecolor(0xfff, 0xfff);
11+
$img2 = imagecreate(0xfff, 0xfff);
12+
imagecolorallocate($img2, 0, 0, 0);
13+
imagesetpixel($img2, 0, 0, 255);
14+
imagecolormatch($img1, $img2);
15+
?>
16+
===DONE===
17+
--EXPECT--
18+
===DONE===

0 commit comments

Comments
 (0)