[php-src] master: ext/gd: report the upper bound of the imagecreate() sizes
| From: | lacatoire via David Carlier | Date: | Thu, 12 Nov 2026 05:08:55 +0000 |
| Subject: | [php-src] master: ext/gd: report the upper bound of the imagecreate() sizes | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143617@lists.php.net to get a copy of this message | ||
Author: lacatoire (lacatoire)
Committer: David Carlier (devnexen)
Date: 2026-11-12T05:03:47Z
Commit: https://github.com/php/php-src/commit/66c0f525665ddacbf33f755e2a938e110d650a50
Raw diff: https://github.com/php/php-src/commit/66c0f525665ddacbf33f755e2a938e110d650a50.diff
ext/gd: report the upper bound of the imagecreate() sizes
imagecreate() and imagecreatetruecolor() reject a width or height of
INT_MAX or more with "must be greater than 0", which is false for
every positive value rejected on the upper side.
Close GH-25386
Changed paths:
A ext/gd/tests/imagecreate_error_oversized.phpt
M ext/gd/gd.c
M ext/gd/tests/imagecreate_error.phpt
M ext/gd/tests/imagecreatetruecolor_error2.phpt
Diff:
diff --git a/ext/gd/gd.c b/ext/gd/gd.c
index 5bcf11846316..68e62d9a1e29 100644
--- a/ext/gd/gd.c
+++ b/ext/gd/gd.c
@@ -693,12 +693,12 @@ PHP_FUNCTION(imagecreatetruecolor)
ZEND_PARSE_PARAMETERS_END();
if (x_size <= 0 || x_size >= INT_MAX) {
- zend_argument_value_error(1, "must be greater than 0");
+ zend_argument_value_error(1, "must be greater than 0 and less than %d", INT_MAX);
RETURN_THROWS();
}
if (y_size <= 0 || y_size >= INT_MAX) {
- zend_argument_value_error(2, "must be greater than 0");
+ zend_argument_value_error(2, "must be greater than 0 and less than %d", INT_MAX);
RETURN_THROWS();
}
@@ -1355,12 +1355,12 @@ PHP_FUNCTION(imagecreate)
ZEND_PARSE_PARAMETERS_END();
if (x_size <= 0 || x_size >= INT_MAX) {
- zend_argument_value_error(1, "must be greater than 0");
+ zend_argument_value_error(1, "must be greater than 0 and less than %d", INT_MAX);
RETURN_THROWS();
}
if (y_size <= 0 || y_size >= INT_MAX) {
- zend_argument_value_error(2, "must be greater than 0");
+ zend_argument_value_error(2, "must be greater than 0 and less than %d", INT_MAX);
RETURN_THROWS();
}
diff --git a/ext/gd/tests/imagecreate_error.phpt b/ext/gd/tests/imagecreate_error.phpt
index c6a938546874..1abf16d19846 100644
--- a/ext/gd/tests/imagecreate_error.phpt
+++ b/ext/gd/tests/imagecreate_error.phpt
@@ -17,6 +17,6 @@ trycatch_dump(
);
?>
---EXPECT--
-!! [ValueError] imagecreate(): Argument #1 ($width) must be greater than 0
-!! [ValueError] imagecreate(): Argument #2 ($height) must be greater than 0
+--EXPECTF--
+!! [ValueError] imagecreate(): Argument #1 ($width) must be greater than 0 and less than %d
+!! [ValueError] imagecreate(): Argument #2 ($height) must be greater than 0 and less than %d
diff --git a/ext/gd/tests/imagecreate_error_oversized.phpt
b/ext/gd/tests/imagecreate_error_oversized.phpt
new file mode 100644
index 000000000000..333a6e7c2086
--- /dev/null
+++ b/ext/gd/tests/imagecreate_error_oversized.phpt
@@ -0,0 +1,26 @@
+--TEST--
+imagecreate()/imagecreatetruecolor(): oversized dimensions produce a correct error message
+--EXTENSIONS--
+gd
+--FILE--
+<?php
+foreach ([PHP_INT_MAX, 2147483647, 4294967296] as $v) {
+ try { imagecreate($v, 1); } catch (Throwable $e) { echo $e::class, ': ',
$e->getMessage(), "\n"; }
+ try { imagecreate(1, $v); } catch (Throwable $e) { echo $e::class, ': ',
$e->getMessage(), "\n"; }
+ try { imagecreatetruecolor($v, 1); } catch (Throwable $e) { echo $e::class, ': ',
$e->getMessage(), "\n"; }
+ try { imagecreatetruecolor(1, $v); } catch (Throwable $e) { echo $e::class, ': ',
$e->getMessage(), "\n"; }
+}
+?>
+--EXPECTF--
+ValueError: imagecreate(): Argument #1 ($width) must be greater than 0 and less than %d
+ValueError: imagecreate(): Argument #2 ($height) must be greater than 0 and less than %d
+ValueError: imagecreatetruecolor(): Argument #1 ($width) must be greater than 0 and less than %d
+ValueError: imagecreatetruecolor(): Argument #2 ($height) must be greater than 0 and less than %d
+ValueError: imagecreate(): Argument #1 ($width) must be greater than 0 and less than %d
+ValueError: imagecreate(): Argument #2 ($height) must be greater than 0 and less than %d
+ValueError: imagecreatetruecolor(): Argument #1 ($width) must be greater than 0 and less than %d
+ValueError: imagecreatetruecolor(): Argument #2 ($height) must be greater than 0 and less than %d
+ValueError: imagecreate(): Argument #1 ($width) must be greater than 0 and less than %d
+ValueError: imagecreate(): Argument #2 ($height) must be greater than 0 and less than %d
+ValueError: imagecreatetruecolor(): Argument #1 ($width) must be greater than 0 and less than %d
+ValueError: imagecreatetruecolor(): Argument #2 ($height) must be greater than 0 and less than %d
diff --git a/ext/gd/tests/imagecreatetruecolor_error2.phpt
b/ext/gd/tests/imagecreatetruecolor_error2.phpt
index 0d299d3c11bb..f09d6adbc429 100644
--- a/ext/gd/tests/imagecreatetruecolor_error2.phpt
+++ b/ext/gd/tests/imagecreatetruecolor_error2.phpt
@@ -19,6 +19,6 @@ trycatch_dump(
);
?>
---EXPECT--
-!! [ValueError] imagecreatetruecolor(): Argument #1 ($width) must be greater than 0
-!! [ValueError] imagecreatetruecolor(): Argument #2 ($height) must be greater than 0
+--EXPECTF--
+!! [ValueError] imagecreatetruecolor(): Argument #1 ($width) must be greater than 0 and less than
%d
+!! [ValueError] imagecreatetruecolor(): Argument #2 ($height) must be greater than 0 and less than
%d