Skip to content

Fix GH-17772: imagepalettetotruecolor segfault on invalid truecolor p… #17777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/gd/libgd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,11 @@ int gdImagePaletteToTrueColor(gdImagePtr src)
const unsigned int sy = gdImageSY(src);
const unsigned int sx = gdImageSX(src);

src->tpixels = (int **) gdMalloc(sizeof(int *) * sy);
// Note: do not revert back to gdMalloc() below ; reason here,
// due to a bug with a certain memory_limit INI value treshold,
// imagepalettetotruecolor crashes with even unrelated ZendMM allocations.
// See GH-17772 for an use case.
src->tpixels = (int **) gdCalloc(sizeof(int *), sy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment that this is an intended change, so it won't be inadvertently dropped when syncing with upstream.

if (src->tpixels == NULL) {
return 0;
}
Expand Down
28 changes: 28 additions & 0 deletions ext/gd/tests/gh17772.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
GH-17772 (imagepalettetotruecolor segfault on image deallocation)
--EXTENSIONS--
gd
--INI--
memory_limit=2M
--CREDITS--
YuanchengJiang
--SKIPIF--
<?php
if (!GD_BUNDLED) die("skip requires bundled GD library");
?>
--FILE--
<?php
function setStyleAndThickness($im, $color, $thickness)
{
$arr = [];
$i = 0;
while ($i < 16 * $thickness) {
$arer[$i++] = $color;
}
}
$im = imagecreate(800, 800);
setStyleAndThickness($im, 0, 6);
imagepalettetotruecolor($im);
?>
--EXPECTF--
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since that will only happen with bundled libgd, the test should be skipped if !GD_BUNDLED.

Loading