Skip to content

Commit 7e97325

Browse files
committed
ext/gd: checking imagescale/imagefilter invalid values.
1 parent af29403 commit 7e97325

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

ext/gd/gd.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3645,6 +3645,16 @@ static void php_image_filter_scatter(INTERNAL_FUNCTION_PARAMETERS)
36453645
Z_PARAM_ARRAY(hash_colors)
36463646
ZEND_PARSE_PARAMETERS_END();
36473647

3648+
if (ZEND_SIZE_T_INT_OVFL(scatter_sub)) {
3649+
zend_argument_value_error(3, "must not be greater than %d", INT_MAX);
3650+
RETURN_THROWS();
3651+
}
3652+
3653+
if (ZEND_SIZE_T_INT_OVFL(scatter_plus)) {
3654+
zend_argument_value_error(4, "must not be greater than %d", INT_MAX);
3655+
RETURN_THROWS();
3656+
}
3657+
36483658
im = php_gd_libgdimageptr_from_zval_p(IM);
36493659

36503660
if (hash_colors) {
@@ -3939,6 +3949,12 @@ PHP_FUNCTION(imagescale)
39393949
Z_PARAM_LONG(tmp_h)
39403950
Z_PARAM_LONG(tmp_m)
39413951
ZEND_PARSE_PARAMETERS_END();
3952+
3953+
if (tmp_m < GD_DEFAULT || tmp_m >= GD_METHOD_COUNT) {
3954+
zend_argument_value_error(4, "must be a valid mode");
3955+
RETURN_THROWS();
3956+
}
3957+
39423958
method = tmp_m;
39433959

39443960
im = php_gd_libgdimageptr_from_zval_p(IM);
@@ -3958,10 +3974,21 @@ PHP_FUNCTION(imagescale)
39583974
}
39593975
}
39603976

3961-
if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) {
3977+
if (tmp_w <= 0) {
3978+
RETURN_FALSE;
3979+
} else if (ZEND_SIZE_T_INT_OVFL(tmp_w)) {
3980+
zend_argument_value_error(2, "must be lower or equal to %d", INT_MAX);
3981+
RETURN_THROWS();
3982+
}
3983+
3984+
if (tmp_h <= 0) {
39623985
RETURN_FALSE;
3986+
} else if (ZEND_SIZE_T_INT_OVFL(tmp_h)) {
3987+
zend_argument_value_error(3, "must be lower or equal to %d", INT_MAX);
3988+
RETURN_THROWS();
39633989
}
39643990

3991+
39653992
new_width = tmp_w;
39663993
new_height = tmp_h;
39673994

ext/gd/tests/bug72337.phpt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ gd
55
--FILE--
66
<?php
77
$im = imagecreatetruecolor(1, 1);
8+
try {
9+
imagescale($im, 0, 0, -10);
10+
} catch (\ValueError $e) {
11+
echo $e->getMessage() . PHP_EOL;
12+
}
813
imagescale($im, 0, 0, IMG_BICUBIC_FIXED);
914
echo "OK";
1015
?>
1116
--EXPECT--
17+
imagescale(): Argument #4 ($mode) must be a valid mode
1218
OK

ext/gd/tests/bug73957.phpt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only');
99
--FILE--
1010
<?php
1111
$im = imagecreate(8, 8);
12-
$im = imagescale($im, 0x100000001, 1);
13-
var_dump($im);
14-
if ($im) { // which is not supposed to happen
15-
var_dump(imagesx($im));
12+
13+
try {
14+
$im = imagescale($im, 0x100000001, 1);
15+
// which is not supposed to happen
16+
var_dump(imagesx($im));
17+
} catch (\ValueError $e) {
18+
echo $e->getMessage();
1619
}
1720
?>
18-
--EXPECT--
19-
bool(false)
21+
--EXPECTF--
22+
imagescale(): Argument #2 ($width) must be lower or equal to %d

0 commit comments

Comments
 (0)