From: Mark Date: Tue, 3 Sep 2019 23:55:31 +0000 (+0200) Subject: Warnings to Errors imagecreate(truecolor) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fde52e8a2c2a485b0370db5f7e26236807ba63cf;p=php Warnings to Errors imagecreate(truecolor) We also add a test helper which we will be using for other GD functions as well. --- diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 35edc9403c..75bbb1da9d 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -828,9 +828,14 @@ PHP_FUNCTION(imagecreatetruecolor) return; } - if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Invalid image dimensions"); - RETURN_FALSE; + if (x_size <= 0 || x_size >= INT_MAX) { + zend_throw_error(NULL, "Invalid width (x_size)"); + return; + } + + if (y_size <= 0 || y_size >= INT_MAX) { + zend_throw_error(NULL, "Invalid height (y_size)"); + return; } im = gdImageCreateTrueColor(x_size, y_size); @@ -1466,9 +1471,14 @@ PHP_FUNCTION(imagecreate) return; } - if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Invalid image dimensions"); - RETURN_FALSE; + if (x_size <= 0 || x_size >= INT_MAX) { + zend_throw_error(NULL, "Invalid width (x_size)"); + return; + } + + if (y_size <= 0 || y_size >= INT_MAX) { + zend_throw_error(NULL, "Invalid height (y_size)"); + return; } im = gdImageCreate(x_size, y_size); diff --git a/ext/gd/tests/func.inc b/ext/gd/tests/func.inc index 34d9c47155..faa0b06816 100644 --- a/ext/gd/tests/func.inc +++ b/ext/gd/tests/func.inc @@ -146,3 +146,21 @@ function save_actual_image($image) $filename = "{$pathinfo['dirname']}/{$pathinfo['filename']}.out.png"; imagepng($image, $filename); } + +/** + * Replicates write errors to the output log, but by catching + * and formatting exceptions instead so they have a consistent + * output + */ + +function trycatch_dump(...$tests) { + foreach ($tests as $test) { + try { + var_dump($test()); + } + catch (\Error $e) { + echo '!! [' . get_class($e) . '] ' . $e->getMessage() . "\n"; + } + } +} + diff --git a/ext/gd/tests/imagecreate_error.phpt b/ext/gd/tests/imagecreate_error.phpt new file mode 100644 index 0000000000..127c43ec57 --- /dev/null +++ b/ext/gd/tests/imagecreate_error.phpt @@ -0,0 +1,21 @@ +--TEST-- +Testing imagecreate(): error on out of bound parameters +--SKIPIF-- + +--FILE-- + imagecreate(-1, 30), + fn() => imagecreate(30, -1) +); + +?> +--EXPECT-- +!! [Error] Invalid width (x_size) +!! [Error] Invalid height (y_size) diff --git a/ext/gd/tests/imagecreatetruecolor_error2.phpt b/ext/gd/tests/imagecreatetruecolor_error2.phpt index 8847333722..55252533b3 100644 --- a/ext/gd/tests/imagecreatetruecolor_error2.phpt +++ b/ext/gd/tests/imagecreatetruecolor_error2.phpt @@ -9,10 +9,15 @@ Rafael Dohms ?> --FILE-- ---EXPECTF-- -Warning: imagecreatetruecolor(): Invalid image dimensions in %s on line %d -Warning: imagecreatetruecolor(): Invalid image dimensions in %s on line %d +require __DIR__ . '/func.inc'; + +trycatch_dump( + fn() => imagecreatetruecolor(-1, 30), + fn() => imagecreatetruecolor(30, -1) +); + +?> +--EXPECT-- +!! [Error] Invalid width (x_size) +!! [Error] Invalid height (y_size)