[php-src] master: Merge branch 'PHP-8.5' into PHP-8.6
| From: | Jakub Zelenka | Date: | Fri, 30 Oct 2026 21:27:50 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.5' into PHP-8.6 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143043@lists.php.net to get a copy of this message | ||
Author: Jakub Zelenka (bukka)
Date: 2026-10-30T22:24:36+01:00
Commit: https://github.com/php/php-src/commit/bbbb258882ef840421e89e968b065d64d9456800
Raw diff: https://github.com/php/php-src/commit/bbbb258882ef840421e89e968b065d64d9456800.diff
Merge branch 'PHP-8.5' into PHP-8.6
* PHP-8.5:
Fix json_encode() depth range check before narrowing
Changed paths:
A ext/json/tests/json_encode_depth.phpt
M NEWS
M ext/json/json.c
Diff:
diff --git a/NEWS b/NEWS
index 3a0d0c0b1d39..24f62c4e91b6 100644
--- a/NEWS
+++ b/NEWS
@@ -59,6 +59,8 @@ PHP NEWS
. Fixed json_encode() leaving the recursion guard set on objects with hooked
properties after running out of memory while escaping a property name.
(adapik)
+ . Fixed json_encode() silently narrowing $depth values outside the int range,
+ which wrapped to an unrelated depth. (cuishuang)
- LDAP:
. Fix crash in ldap_exop_sync() when $response_data is omitted. (lacatoire)
diff --git a/ext/json/json.c b/ext/json/json.c
index 04a62f52152f..b71b9aafe997 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -250,6 +250,11 @@ PHP_FUNCTION(json_encode)
Z_PARAM_LONG(depth)
ZEND_PARSE_PARAMETERS_END();
+ if (ZEND_LONG_EXCEEDS_INT(depth)) {
+ zend_argument_value_error(3, "must be between %d and %d", INT_MIN, INT_MAX);
+ RETURN_THROWS();
+ }
+
php_json_encode_init(&encoder);
encoder.max_depth = (int)depth;
php_json_encode_zval(&buf, parameter, (int)options, &encoder);
diff --git a/ext/json/tests/json_encode_depth.phpt b/ext/json/tests/json_encode_depth.phpt
new file mode 100644
index 000000000000..075228da472c
--- /dev/null
+++ b/ext/json/tests/json_encode_depth.phpt
@@ -0,0 +1,19 @@
+--TEST--
+json_encode() rejects depth values outside the encoder range
+--SKIPIF--
+<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
+--FILE--
+<?php
+
+foreach ([2147483648, -2147483649] as $depth) {
+ try {
+ json_encode([], depth: $depth);
+ } catch (Throwable $e) {
+ echo $e::class, ': ', $e->getMessage(), "\n";
+ }
+}
+
+?>
+--EXPECT--
+ValueError: json_encode(): Argument #3 ($depth) must be between -2147483648 and 2147483647
+ValueError: json_encode(): Argument #3 ($depth) must be between -2147483648 and 2147483647