Skip to content

Commit

Permalink
Fix phpGH-16255: Unexpected nan value in ext/gd/libgd/gd_filter.c
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed Dec 15, 2024
1 parent 1862aff commit ece7bd3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
19 changes: 18 additions & 1 deletion ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,24 @@ PHP_FUNCTION(imageconvolution)
}
}
}
res = gdImageConvolution(im_src, matrix, (float)div, (float)offset);

if (UNEXPECTED(!zend_finite(div))) {
zend_argument_value_error(3, "must be finite");
RETURN_THROWS();
}

float div_float = (float) div;
if (UNEXPECTED(div_float == 0.0f)) {
zend_argument_value_error(3, "must not be 0");
RETURN_THROWS();
}

if (UNEXPECTED(!zend_finite(offset))) {
zend_argument_value_error(4, "must be finite");
RETURN_THROWS();
}

res = gdImageConvolution(im_src, matrix, div_float, (float) offset);

if (res) {
RETURN_TRUE;
Expand Down
34 changes: 34 additions & 0 deletions ext/gd/tests/gh16255.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
GH-16255 (Unexpected nan value in ext/gd/libgd/gd_filter.c)
--EXTENSIONS--
gd
--CREDITS--
cmb69
--FILE--
<?php
$matrix = array(array(1, 0, 1), array(0, 5, 0), array(1, 0, 1));
$im = imagecreatetruecolor(40, 40);

try {
imageconvolution($im, $matrix, NAN, 1.0);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

try {
imageconvolution($im, $matrix, 2.225E-307, 1.0);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

try {
imageconvolution($im, $matrix, 1, NAN);
} catch (ValueError $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
imageconvolution(): Argument #3 ($divisor) must be finite
imageconvolution(): Argument #3 ($divisor) must not be 0
imageconvolution(): Argument #4 ($offset) must be finite

0 comments on commit ece7bd3

Please sign in to comment.