[njs] Parser: fixed return statement parsing with invalid expression.
Dmitry Volyntsev
xeioex at nginx.com
Tue Oct 24 04:39:04 UTC 2023
details: https://hg.nginx.org/njs/rev/c081cc5377a8
branches:
changeset: 2224:c081cc5377a8
user: Vadim Zhestikov <v.zhestikov at f5.com>
date: Mon Oct 23 21:19:03 2023 -0700
description:
Parser: fixed return statement parsing with invalid expression.
diffstat:
src/njs_parser.c | 15 +++++++++------
src/test/njs_unit_test.c | 9 +++++++++
2 files changed, 18 insertions(+), 6 deletions(-)
diffs (51 lines):
diff -r d83c6616f2b1 -r c081cc5377a8 src/njs_parser.c
--- a/src/njs_parser.c Fri Oct 20 08:44:52 2023 -0700
+++ b/src/njs_parser.c Mon Oct 23 21:19:03 2023 -0700
@@ -6347,10 +6347,12 @@ njs_parser_return_statement(njs_parser_t
parser->node = NULL;
- njs_parser_next(parser, njs_parser_expression);
-
- return njs_parser_after(parser, current, node, 0,
- njs_parser_return_statement_after);
+ if (token->type != NJS_TOKEN_CLOSE_BRACE) {
+ njs_parser_next(parser, njs_parser_expression);
+
+ return njs_parser_after(parser, current, node, 0,
+ njs_parser_return_statement_after);
+ }
}
parser->node = node;
@@ -6364,8 +6366,9 @@ njs_parser_return_statement_after(njs_pa
njs_lexer_token_t *token, njs_queue_link_t *current)
{
if (parser->ret != NJS_OK) {
- parser->node = parser->target;
- return njs_parser_stack_pop(parser);
+ njs_parser_syntax_error(parser, "Unexpected token \"%V\"",
+ &token->text);
+ return NJS_DONE;
}
if (njs_parser_expect_semicolon(parser, token) != NJS_OK) {
diff -r d83c6616f2b1 -r c081cc5377a8 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Fri Oct 20 08:44:52 2023 -0700
+++ b/src/test/njs_unit_test.c Mon Oct 23 21:19:03 2023 -0700
@@ -10093,6 +10093,15 @@ static njs_unit_test_t njs_test[] =
{ njs_str("\n{\nreturn;\n}"),
njs_str("SyntaxError: Illegal return statement in 3") },
+ { njs_str("function f () {return a +}"),
+ njs_str("SyntaxError: Unexpected token \"}\" in 1") },
+
+ { njs_str("`${function(){return n=>}}`"),
+ njs_str("SyntaxError: Unexpected token \"}\" in 1") },
+
+ { njs_str("(function(){return a +})"),
+ njs_str("SyntaxError: Unexpected token \"}\" in 1") },
+
{ njs_str("if (1) function f(){}"),
njs_str("SyntaxError: Functions can only be declared at top level or inside a block in 1") },
More information about the nginx-devel
mailing list