]> granicus.if.org Git - php/commitdiff
Warnings to Errors imagecreate(truecolor)
authorMark <mrandall@digitellinc.com>
Tue, 3 Sep 2019 23:55:31 +0000 (01:55 +0200)
committerChristoph M. Becker <cmbecker69@gmx.de>
Wed, 4 Sep 2019 10:09:07 +0000 (12:09 +0200)
We also add a test helper which we will be using for other GD functions
as well.

ext/gd/gd.c
ext/gd/tests/func.inc
ext/gd/tests/imagecreate_error.phpt [new file with mode: 0644]
ext/gd/tests/imagecreatetruecolor_error2.phpt

index 35edc9403cdbe382fb4f70802748dc4318aff992..75bbb1da9d5271bb90550cec18cf03ea0860fc83 100644 (file)
@@ -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);
index 34d9c471550ee17449923bedba62d15b1e907694..faa0b06816223386bc24f03c34d58cbe683e2216 100644 (file)
@@ -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 (file)
index 0000000..127c43e
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+Testing imagecreate(): error on out of bound parameters
+--SKIPIF--
+<?php
+       if (!extension_loaded("gd")) die("skip GD not present");
+       if (!function_exists("imagecreate")) die("skip GD Version not compatible");
+?>
+--FILE--
+<?php
+
+require __DIR__ . '/func.inc';
+
+trycatch_dump(
+    fn() => imagecreate(-1, 30),
+    fn() => imagecreate(30, -1)
+);
+
+?>
+--EXPECT--
+!! [Error] Invalid width (x_size)
+!! [Error] Invalid height (y_size)
index 88473337226c805701f78414a8c0b250047419dc..55252533b3e649418e4f4dfad8d1ad9d61cccbd5 100644 (file)
@@ -9,10 +9,15 @@ Rafael Dohms <rdohms [at] gmail [dot] com>
 ?>
 --FILE--
 <?php
-$image = imagecreatetruecolor(-1, 30);
-$image = imagecreatetruecolor(30, -1);
-?>
---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)