Skip to content

Commit bbbb258

Browse files
committed
Merge branch 'PHP-8.5' into PHP-8.6
* PHP-8.5: Fix json_encode() depth range check before narrowing
2 parents 8a5d674 + 8dace27 commit bbbb258

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ PHP NEWS
5959
. Fixed json_encode() leaving the recursion guard set on objects with hooked
6060
properties after running out of memory while escaping a property name.
6161
(adapik)
62+
. Fixed json_encode() silently narrowing $depth values outside the int range,
63+
which wrapped to an unrelated depth. (cuishuang)
6264

6365
- LDAP:
6466
. Fix crash in ldap_exop_sync() when $response_data is omitted. (lacatoire)

‎ext/json/json.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ PHP_FUNCTION(json_encode)
250250
Z_PARAM_LONG(depth)
251251
ZEND_PARSE_PARAMETERS_END();
252252

253+
if (ZEND_LONG_EXCEEDS_INT(depth)) {
254+
zend_argument_value_error(3, "must be between %d and %d", INT_MIN, INT_MAX);
255+
RETURN_THROWS();
256+
}
257+
253258
php_json_encode_init(&encoder);
254259
encoder.max_depth = (int)depth;
255260
php_json_encode_zval(&buf, parameter, (int)options, &encoder);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
json_encode() rejects depth values outside the encoder range
3+
--SKIPIF--
4+
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
5+
--FILE--
6+
<?php
7+
8+
foreach ([2147483648, -2147483649] as $depth) {
9+
try {
10+
json_encode([], depth: $depth);
11+
} catch (Throwable $e) {
12+
echo $e::class, ': ', $e->getMessage(), "\n";
13+
}
14+
}
15+
16+
?>
17+
--EXPECT--
18+
ValueError: json_encode(): Argument #3 ($depth) must be between -2147483648 and 2147483647
19+
ValueError: json_encode(): Argument #3 ($depth) must be between -2147483648 and 2147483647

0 commit comments

Comments
 (0)