Skip to content

Commit fde52e8

Browse files
marandallcmb69
authored andcommitted
Warnings to Errors imagecreate(truecolor)
We also add a test helper which we will be using for other GD functions as well.
1 parent 616027c commit fde52e8

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed

ext/gd/gd.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,14 @@ PHP_FUNCTION(imagecreatetruecolor)
828828
return;
829829
}
830830

831-
if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) {
832-
php_error_docref(NULL, E_WARNING, "Invalid image dimensions");
833-
RETURN_FALSE;
831+
if (x_size <= 0 || x_size >= INT_MAX) {
832+
zend_throw_error(NULL, "Invalid width (x_size)");
833+
return;
834+
}
835+
836+
if (y_size <= 0 || y_size >= INT_MAX) {
837+
zend_throw_error(NULL, "Invalid height (y_size)");
838+
return;
834839
}
835840

836841
im = gdImageCreateTrueColor(x_size, y_size);
@@ -1466,9 +1471,14 @@ PHP_FUNCTION(imagecreate)
14661471
return;
14671472
}
14681473

1469-
if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) {
1470-
php_error_docref(NULL, E_WARNING, "Invalid image dimensions");
1471-
RETURN_FALSE;
1474+
if (x_size <= 0 || x_size >= INT_MAX) {
1475+
zend_throw_error(NULL, "Invalid width (x_size)");
1476+
return;
1477+
}
1478+
1479+
if (y_size <= 0 || y_size >= INT_MAX) {
1480+
zend_throw_error(NULL, "Invalid height (y_size)");
1481+
return;
14721482
}
14731483

14741484
im = gdImageCreate(x_size, y_size);

ext/gd/tests/func.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,21 @@ function save_actual_image($image)
146146
$filename = "{$pathinfo['dirname']}/{$pathinfo['filename']}.out.png";
147147
imagepng($image, $filename);
148148
}
149+
150+
/**
151+
* Replicates write errors to the output log, but by catching
152+
* and formatting exceptions instead so they have a consistent
153+
* output
154+
*/
155+
156+
function trycatch_dump(...$tests) {
157+
foreach ($tests as $test) {
158+
try {
159+
var_dump($test());
160+
}
161+
catch (\Error $e) {
162+
echo '!! [' . get_class($e) . '] ' . $e->getMessage() . "\n";
163+
}
164+
}
165+
}
166+

ext/gd/tests/imagecreate_error.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Testing imagecreate(): error on out of bound parameters
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("gd")) die("skip GD not present");
6+
if (!function_exists("imagecreate")) die("skip GD Version not compatible");
7+
?>
8+
--FILE--
9+
<?php
10+
11+
require __DIR__ . '/func.inc';
12+
13+
trycatch_dump(
14+
fn() => imagecreate(-1, 30),
15+
fn() => imagecreate(30, -1)
16+
);
17+
18+
?>
19+
--EXPECT--
20+
!! [Error] Invalid width (x_size)
21+
!! [Error] Invalid height (y_size)

ext/gd/tests/imagecreatetruecolor_error2.phpt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ Rafael Dohms <rdohms [at] gmail [dot] com>
99
?>
1010
--FILE--
1111
<?php
12-
$image = imagecreatetruecolor(-1, 30);
13-
$image = imagecreatetruecolor(30, -1);
14-
?>
15-
--EXPECTF--
16-
Warning: imagecreatetruecolor(): Invalid image dimensions in %s on line %d
1712

18-
Warning: imagecreatetruecolor(): Invalid image dimensions in %s on line %d
13+
require __DIR__ . '/func.inc';
14+
15+
trycatch_dump(
16+
fn() => imagecreatetruecolor(-1, 30),
17+
fn() => imagecreatetruecolor(30, -1)
18+
);
19+
20+
?>
21+
--EXPECT--
22+
!! [Error] Invalid width (x_size)
23+
!! [Error] Invalid height (y_size)

0 commit comments

Comments
 (0)