Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ext/json/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions ext/json/tests/json_encode_depth.phpt
Original file line number Diff line number Diff line change
@@ -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
Loading