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

Conversation

devnexen
Copy link
Member

…ixel.

@devnexen devnexen marked this pull request as ready for review February 13, 2025 12:04
@cmb69
Copy link
Member

cmb69 commented Feb 13, 2025

Ugh, that's ugly, since it only affects bundled libgd which uses ZendMM; there appear to be no issues with external libgd (which doesn't heed memory_limit anyway). So it's not likely that we can have a fix being applied upstream, but further down the road some sync might get rid of our fix (should probably add a comment, that gdCalloc() needs to stay).

Possibly even worse, if upstream ever implements custom allocators (libgd/libgd#335), in which case such issues (I expect more than this particular one) would also affect them.

The general problem I'm seeing here is that libgd never came around to actually implement a contiguous buffer for the pixels (although that is planned for many, many years), so you would have a single allocation upfront, instead of allocating an individual buffer for each row.

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.

@devnexen devnexen linked an issue Feb 13, 2025 that may be closed by this pull request
Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

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

Besides the nits below, this looks good to me.

@@ -3108,7 +3108,7 @@ int gdImagePaletteToTrueColor(gdImagePtr src)
const unsigned int sy = gdImageSY(src);
const unsigned int sx = gdImageSX(src);

src->tpixels = (int **) gdMalloc(sizeof(int *) * sy);
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.

YuanchengJiang
--SKIPIF--
<?php
if (!GD_BUNDLED) die("skip requires bundled GD library\n");
Copy link
Member

Choose a reason for hiding this comment

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

I think that trailing \n is superfluous at best.

Suggested change
if (!GD_BUNDLED) die("skip requires bundled GD library\n");
if (!GD_BUNDLED) die("skip requires bundled GD library");

@devnexen devnexen closed this in 4d7d01d Feb 14, 2025
@devnexen devnexen deleted the gh17772 branch February 16, 2025 10:02
charmitro pushed a commit to wasix-org/php that referenced this pull request Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SEGV Zend/zend_alloc.c
2 participants