diff --git a/ext/json/json_parser.y b/ext/json/json_parser.y index 360141f9d034..23c64bd03f34 100644 --- a/ext/json/json_parser.y +++ b/ext/json/json_parser.y @@ -328,7 +328,12 @@ static int php_json_yylex(union YYSTYPE *value, php_json_parser *parser) static void php_json_yyerror(php_json_parser *parser, char const *msg) { if (!parser->scanner.errcode) { - parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX; + /* Bison reports that its stack limit was hit as "memory exhausted", the input is not malformed */ + if (strcmp(msg, "memory exhausted") == 0) { + parser->scanner.errcode = PHP_JSON_ERROR_DEPTH; + } else { + parser->scanner.errcode = PHP_JSON_ERROR_SYNTAX; + } } } diff --git a/ext/json/tests/json_parser_stack_limit.phpt b/ext/json/tests/json_parser_stack_limit.phpt new file mode 100644 index 000000000000..962e9181b28d --- /dev/null +++ b/ext/json/tests/json_parser_stack_limit.phpt @@ -0,0 +1,21 @@ +--TEST-- +JSON: nesting beyond the parser stack is reported as a depth error, not a syntax error +--FILE-- + +--EXPECT-- +NULL +bool(true) +bool(false) +bool(true) +bool(false) +bool(true)