Skip to content

Commit

Permalink
Fix phpGH-16322: imageaffine overflow on affine argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Oct 10, 2024
1 parent 1ee56bd commit 7590da5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3687,13 +3687,25 @@ PHP_FUNCTION(imageaffine)
if ((zval_affine_elem = zend_hash_index_find(Z_ARRVAL_P(z_affine), i)) != NULL) {
switch (Z_TYPE_P(zval_affine_elem)) {
case IS_LONG:
affine[i] = Z_LVAL_P(zval_affine_elem);
affine[i] = Z_LVAL_P(zval_affine_elem);
if (affine[i] < INT_MIN || affine[i] > INT_MAX) {
zend_argument_value_error(2, "element %i must be between %d and %d", i, INT_MIN, INT_MAX);
RETURN_THROWS();
}
break;
case IS_DOUBLE:
affine[i] = Z_DVAL_P(zval_affine_elem);
if (affine[i] < INT_MIN || affine[i] > INT_MAX) {
zend_argument_value_error(2, "element %i must be between %d and %d", i, INT_MIN, INT_MAX);
RETURN_THROWS();
}
break;
case IS_STRING:
affine[i] = zval_get_double(zval_affine_elem);
if (affine[i] < INT_MIN || affine[i] > INT_MAX) {
zend_argument_value_error(2, "element %i must be between %d and %d", i, INT_MIN, INT_MAX);
RETURN_THROWS();
}
break;
default:
zend_argument_type_error(3, "contains invalid type for element %i", i);
Expand Down
27 changes: 27 additions & 0 deletions ext/gd/tests/gh16322.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-16322 (imageaffine overflow/underflow on affine matrix)
--EXTENSIONS--
gd
--INI--
memory_limit=-1
--FILE--
<?php
$matrix = [PHP_INT_MAX, 1, 1, 1, 1, 1];
$src = imagecreatetruecolor(8, 8);

try {
imageaffine($src, $matrix);
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;
}
$matrix[0] = 1;
$matrix[3] = PHP_INT_MIN;
try {
imageaffine($src, $matrix);
} catch (\ValueError $e) {
echo $e->getMessage();
}
?>
--EXPECTF--
imageaffine(): Argument #2 ($affine) element 0 must be between %s and %d
imageaffine(): Argument #2 ($affine) element 3 must be between %s and %d

0 comments on commit 7590da5

Please sign in to comment.