Skip to content

ext/gd: iamgeresolution checks overflow. #14585

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

Merged
merged 3 commits into from
Jun 16, 2024
Merged
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
16 changes: 16 additions & 0 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4282,12 +4282,28 @@ PHP_FUNCTION(imageresolution)
im = php_gd_libgdimageptr_from_zval_p(IM);

if (!res_x_is_null && !res_y_is_null) {
if (res_x < 0 || ZEND_SIZE_T_UINT_OVFL(res_x)) {
zend_argument_value_error(2, "must be between 0 and %u", UINT_MAX);
RETURN_THROWS();
}
if (res_y < 0 || ZEND_SIZE_T_UINT_OVFL(res_y)) {
zend_argument_value_error(3, "must be between 0 and %u", UINT_MAX);
RETURN_THROWS();
}
gdImageSetResolution(im, res_x, res_y);
RETURN_TRUE;
} else if (!res_x_is_null && res_y_is_null) {
if (res_x < 0 || ZEND_SIZE_T_UINT_OVFL(res_x)) {
zend_argument_value_error(2, "must be between 0 and %u", UINT_MAX);
RETURN_THROWS();
}
gdImageSetResolution(im, res_x, res_x);
RETURN_TRUE;
} else if (res_x_is_null && !res_y_is_null) {
if (res_y < 0 || ZEND_SIZE_T_UINT_OVFL(res_y)) {
zend_argument_value_error(3, "must be between 0 and %u", UINT_MAX);
RETURN_THROWS();
}
gdImageSetResolution(im, res_y, res_y);
RETURN_TRUE;
}
Expand Down
34 changes: 34 additions & 0 deletions ext/gd/tests/imageresolution_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Wrong image resolution
--EXTENSIONS--
gd
--SKIPIF--
<?php
if (PHP_INT_SIZE != 8) die("skip on non 64 bits architectures");
?>
--FILE--
<?php
$filename = __DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_png.png';

$exp = imagecreate(100, 100);
imagecolorallocate($exp, 255, 127, 64);

$res = imageresolution($exp);

try {
imageresolution($exp, PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
imageresolution($exp, 127, -PHP_INT_MAX);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
imageresolution($exp, 0, 0);
var_dump(imageresolution($exp) == $res);
?>
--EXPECTF--
imageresolution(): Argument #2 ($resolution_x) must be between 0 and %d
imageresolution(): Argument #3 ($resolution_y) must be between 0 and %d
bool(true)
Loading