Skip to content

Commit 6b0f6d1

Browse files
bug #421 Fix json_validate rejecting valid maximum depth (Emix33)
This PR was squashed before being merged into the 1.27-dev branch. Discussion ---------- Fix json_validate rejecting valid maximum depth Even though the error message for json_decode will say 'Argument #3 ($depth) must be less than 2147483647', the actual behavior of the method allows this (https://3v4l.org/QdfHt) and the official online documentation also confirms this behavior (https://www.php.net/manual/en/function.json-decode.php ' The value must be greater than 0, and less than or equal to 2147483647'). I have not been able to confirm whether 2147483647 is accepted by json_decode on a 32 bit system (where it is equal to PHP_MAX_INT) as I do not have access to one. (3v4l also uses 64 bit PHP) Commits ------- 859ac4f Fix json_validate rejecting valid maximum depth
2 parents 6bbe0d5 + 859ac4f commit 6b0f6d1

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/Php83/Php83.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function json_validate(string $json, int $depth = 512, int $flags
3030
throw new \ValueError('json_validate(): Argument #2 ($depth) must be greater than 0');
3131
}
3232

33-
if ($depth >= self::JSON_MAX_DEPTH) {
33+
if ($depth > self::JSON_MAX_DEPTH) {
3434
throw new \ValueError(sprintf('json_validate(): Argument #2 ($depth) must be less than %d', self::JSON_MAX_DEPTH));
3535
}
3636

tests/Php83/Php83Test.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static function jsonDataProvider(): iterable
4646
yield [true, '{ "test": {"foo": "bar"}, "test2": {"foo" : "bar" }, "test3": {"foo" : "bar" } }'];
4747
yield [false, '{"key1":"value1", "key2":"value2"}', 'Maximum stack depth exceeded', 1];
4848
yield [false, "\"a\xb0b\"", 'Malformed UTF-8 characters, possibly incorrectly encoded'];
49+
yield [true, '{ "test": { "foo": "bar" } }', 'No error', 2147483647];
4950

5051
if (\defined('JSON_INVALID_UTF8_IGNORE')) {
5152
yield [true, "\"a\xb0b\"", 'No error', 512, \JSON_INVALID_UTF8_IGNORE];
@@ -73,8 +74,9 @@ public function testInvalidOptionsProvided(int $depth, int $flags, string $expec
7374
public static function invalidOptionsProvider(): iterable
7475
{
7576
yield [0, 0, 'json_validate(): Argument #2 ($depth) must be greater than 0'];
76-
yield [\PHP_INT_MAX, 0, 'json_validate(): Argument #2 ($depth) must be less than 2147483647'];
77-
77+
if (\PHP_INT_MAX > 2147483647) {
78+
yield [\PHP_INT_MAX, 0, 'json_validate(): Argument #2 ($depth) must be less than 2147483647'];
79+
}
7880
if (\defined('JSON_INVALID_UTF8_IGNORE')) {
7981
yield [
8082
512,

0 commit comments

Comments
 (0)