Skip to content

Commit 1bcfc83

Browse files
committed
Fix GH-16322: imageaffine overflow on affine argument.
1 parent 1ee56bd commit 1bcfc83

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

ext/gd/gd.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3687,13 +3687,25 @@ PHP_FUNCTION(imageaffine)
36873687
if ((zval_affine_elem = zend_hash_index_find(Z_ARRVAL_P(z_affine), i)) != NULL) {
36883688
switch (Z_TYPE_P(zval_affine_elem)) {
36893689
case IS_LONG:
3690-
affine[i] = Z_LVAL_P(zval_affine_elem);
3690+
affine[i] = Z_LVAL_P(zval_affine_elem);
3691+
if (affine[i] < INT_MIN || affine[i] > INT_MAX) {
3692+
zend_argument_value_error(2, "element %i must be between %d and %d", i, INT_MIN, INT_MAX);
3693+
RETURN_THROWS();
3694+
}
36913695
break;
36923696
case IS_DOUBLE:
36933697
affine[i] = Z_DVAL_P(zval_affine_elem);
3698+
if (affine[i] < INT_MIN || affine[i] > INT_MAX) {
3699+
zend_argument_value_error(2, "element %i must be between %d and %d", i, INT_MIN, INT_MAX);
3700+
RETURN_THROWS();
3701+
}
36943702
break;
36953703
case IS_STRING:
36963704
affine[i] = zval_get_double(zval_affine_elem);
3705+
if (affine[i] < INT_MIN || affine[i] > INT_MAX) {
3706+
zend_argument_value_error(2, "element %i must be between %d and %d", i, INT_MIN, INT_MAX);
3707+
RETURN_THROWS();
3708+
}
36973709
break;
36983710
default:
36993711
zend_argument_type_error(3, "contains invalid type for element %i", i);

ext/gd/tests/gh16322.phpt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
GH-16322 (imageaffine overflow/underflow on affine matrix)
3+
--EXTENSIONS--
4+
gd
5+
--INI--
6+
memory_limit=-1
7+
--SKIPIF--
8+
<?php if (PHP_INT_SIZE != 8) die('skip this test is for 64bit platforms only'); ?>
9+
--FILE--
10+
<?php
11+
$matrix = [PHP_INT_MAX, 1, 1, 1, 1, 1];
12+
$src = imagecreatetruecolor(8, 8);
13+
14+
try {
15+
imageaffine($src, $matrix);
16+
} catch (\ValueError $e) {
17+
echo $e->getMessage() . PHP_EOL;
18+
}
19+
$matrix[0] = 1;
20+
$matrix[3] = PHP_INT_MIN;
21+
try {
22+
imageaffine($src, $matrix);
23+
} catch (\ValueError $e) {
24+
echo $e->getMessage();
25+
}
26+
?>
27+
--EXPECTF--
28+
imageaffine(): Argument #2 ($affine) element 0 must be between %s and %d
29+
imageaffine(): Argument #2 ($affine) element 3 must be between %s and %d

0 commit comments

Comments
 (0)