Welcome! Log In Create A New Profile

Advanced

[njs] Parser: fixed return statement parsing with invalid expression.

Dmitry Volyntsev
October 24, 2023 12:40AM
details: https://hg.nginx.org/njs/rev/c081cc5377a8
branches:
changeset: 2224:c081cc5377a8
user: Vadim Zhestikov <v.zhestikov@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") },

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[njs] Parser: fixed return statement parsing with invalid expression.

Dmitry Volyntsev 462 October 24, 2023 12:40AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 273
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready