Skip to content

Commit f2859a4

Browse files
committed
Fix phpGH-16322: imageaffine overflow on affine argument.
close phpGH-16334
1 parent c34d4fb commit f2859a4

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ PHP NEWS
1717
. Fixed bug GH-16316 (DOMXPath breaks when not initialized properly).
1818
(nielsdos)
1919

20+
- GD:
21+
. Fixed bug GH-16334 (imageaffine overflow on matrix elements).
22+
(David Carlier)
23+
2024
- MBstring:
2125
. Fixed bug GH-16361 (mb_substr overflow on start/length arguments).
2226
(David Carlier)

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
GH-16322 (imageaffine overflow/underflow on affine matrix)
3+
--EXTENSIONS--
4+
gd
5+
--INI--
6+
memory_limit=-1
7+
--FILE--
8+
<?php
9+
$matrix = [INF, 1, 1, 1, 1, 1];
10+
$src = imagecreatetruecolor(8, 8);
11+
12+
try {
13+
imageaffine($src, $matrix);
14+
} catch (\ValueError $e) {
15+
echo $e->getMessage() . PHP_EOL;
16+
}
17+
$matrix[0] = 1;
18+
$matrix[3] = -INF;
19+
try {
20+
imageaffine($src, $matrix);
21+
} catch (\ValueError $e) {
22+
echo $e->getMessage();
23+
}
24+
?>
25+
--EXPECTF--
26+
imageaffine(): Argument #2 ($affine) element 0 must be between %s and %d
27+
imageaffine(): Argument #2 ($affine) element 3 must be between %s and %d

0 commit comments

Comments
 (0)