Skip to content

Commit caf23d2

Browse files
committed
Fix deeply nested valid JSON being reported as a syntax error
1 parent e51fa6a commit caf23d2

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎ext/json/json_parser.y‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ static int php_json_yylex(union YYSTYPE *value, php_json_parser *parser)
325325
static void php_json_yyerror(php_json_parser *parser, char const *msg)
326326
{
327327
if (!parser->scanner.errcode) {
328-
parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX;
328+
/* Bison reports that its stack limit was hit as "memory exhausted", the input is not malformed */
329+
if (strcmp(msg, "memory exhausted") == 0) {
330+
parser->scanner.errcode = PHP_JSON_ERROR_DEPTH;
331+
} else {
332+
parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX;
333+
}
329334
}
330335
}
331336

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
JSON: nesting beyond the parser stack is reported as a depth error, not a syntax error
3+
--FILE--
4+
<?php
5+
$deep = str_repeat('[', 100000) . str_repeat(']', 100000);
6+
var_dump(json_decode($deep, true, 2147483647));
7+
var_dump(json_last_error() === JSON_ERROR_DEPTH);
8+
var_dump(json_validate($deep, 2147483647));
9+
var_dump(json_last_error() === JSON_ERROR_DEPTH);
10+
11+
// Invalid input is still a syntax error
12+
var_dump(json_validate('[1,]', 2147483647));
13+
var_dump(json_last_error() === JSON_ERROR_SYNTAX);
14+
?>
15+
--EXPECT--
16+
NULL
17+
bool(true)
18+
bool(false)
19+
bool(true)
20+
bool(false)
21+
bool(true)

0 commit comments

Comments
 (0)