Skip to content

Commit 4d7d01d

Browse files
committed
Fix GH-17772: imagepalettetotruecolor segfault on invalid truecolor pixel.
close GH-17777
1 parent a54af45 commit 4d7d01d

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ PHP NEWS
1818
. Fixed bug GH-17643 (FPM with httpd ProxyPass encoded PATH_INFO env).
1919
(Jakub Zelenka)
2020

21+
- GD:
22+
. Fixed bug GH-17772 (imagepalettetotruecolor crash with memory_limit=2M).
23+
(David Carlier)
24+
2125
- LDAP:
2226
. Fixed bug GH-17704 (ldap_search fails when $attributes contains a
2327
non-packed array with numerical keys). (nielsdos, 7u83)

ext/gd/libgd/gd.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,11 @@ int gdImagePaletteToTrueColor(gdImagePtr src)
31083108
const unsigned int sy = gdImageSY(src);
31093109
const unsigned int sx = gdImageSX(src);
31103110

3111-
src->tpixels = (int **) gdMalloc(sizeof(int *) * sy);
3111+
// Note: do not revert back to gdMalloc() below ; reason here,
3112+
// due to a bug with a certain memory_limit INI value treshold,
3113+
// imagepalettetotruecolor crashes with even unrelated ZendMM allocations.
3114+
// See GH-17772 for an use case.
3115+
src->tpixels = (int **) gdCalloc(sizeof(int *), sy);
31123116
if (src->tpixels == NULL) {
31133117
return 0;
31143118
}

ext/gd/tests/gh17772.phpt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GH-17772 (imagepalettetotruecolor segfault on image deallocation)
3+
--EXTENSIONS--
4+
gd
5+
--INI--
6+
memory_limit=2M
7+
--CREDITS--
8+
YuanchengJiang
9+
--SKIPIF--
10+
<?php
11+
if (!GD_BUNDLED) die("skip requires bundled GD library");
12+
?>
13+
--FILE--
14+
<?php
15+
function setStyleAndThickness($im, $color, $thickness)
16+
{
17+
$arr = [];
18+
$i = 0;
19+
while ($i < 16 * $thickness) {
20+
$arer[$i++] = $color;
21+
}
22+
}
23+
$im = imagecreate(800, 800);
24+
setStyleAndThickness($im, 0, 6);
25+
imagepalettetotruecolor($im);
26+
?>
27+
--EXPECTF--
28+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d

0 commit comments

Comments
 (0)