[njs] Fixed parsing of for-in loops.

Vadim Zhestikov v.zhestikov at f5.com
Sat May 27 02:45:30 UTC 2023


details:   https://hg.nginx.org/njs/rev/b9d18d4dd34e
branches:  
changeset: 2138:b9d18d4dd34e
user:      Vadim Zhestikov <v.zhestikov at f5.com>
date:      Fri May 26 19:43:24 2023 -0700
description:
Fixed parsing of for-in loops.

This fixes parsing for input like: for (a(b * in d) ;

The issue was introduced in 283ae119d121 (0.7.9).

diffstat:

 src/njs_parser.c         |  35 ++++++++++++++++++++++++++---------
 src/test/njs_unit_test.c |   6 ++++++
 2 files changed, 32 insertions(+), 9 deletions(-)

diffs (92 lines):

diff -r e74e56120102 -r b9d18d4dd34e src/njs_parser.c
--- a/src/njs_parser.c	Fri May 26 19:13:41 2023 -0700
+++ b/src/njs_parser.c	Fri May 26 19:43:24 2023 -0700
@@ -294,7 +294,10 @@ static njs_int_t njs_parser_while_after(
 
 static njs_int_t njs_parser_iteration_statement_for(njs_parser_t *parser,
     njs_lexer_token_t *token, njs_queue_link_t *current);
-static njs_int_t njs_parser_for_left_hand_side_expression_map(
+static njs_int_t njs_parser_for_expression_map_continue(
+    njs_parser_t *parser, njs_lexer_token_t *token,
+    njs_queue_link_t *current);
+static njs_int_t njs_parser_for_expression_map_reparse(
     njs_parser_t *parser, njs_lexer_token_t *token,
     njs_queue_link_t *current);
 static njs_int_t njs_parser_expression_continue_op(njs_parser_t *parser,
@@ -5488,12 +5491,10 @@ njs_parser_iteration_statement_for(njs_p
 
 
 static njs_int_t
-njs_parser_for_left_hand_side_expression_map(njs_parser_t *parser,
+njs_parser_for_expression_map_reparse(njs_parser_t *parser,
     njs_lexer_token_t *token, njs_queue_link_t *current)
 {
-    njs_int_t          operation;
-    njs_str_t          *text;
-    njs_parser_node_t  *node;
+    njs_str_t  *text;
 
     if (parser->node == NULL) {
         njs_lexer_in_fail_set(parser->lexer, 1);
@@ -5514,8 +5515,19 @@ njs_parser_for_left_hand_side_expression
 
         return njs_parser_after(parser, current, text, 1,
                                 njs_parser_for_var_in_of_expression);
-
-    }
+    }
+
+    return njs_parser_stack_pop(parser);
+}
+
+
+static njs_int_t
+njs_parser_for_expression_map_continue(njs_parser_t *parser,
+    njs_lexer_token_t *token, njs_queue_link_t *current)
+{
+    njs_int_t          operation;
+    njs_str_t          *text;
+    njs_parser_node_t  *node;
 
     if (token->type != NJS_TOKEN_IN) {
         njs_lexer_in_fail_set(parser->lexer, 1);
@@ -5575,7 +5587,6 @@ njs_parser_for_left_hand_side_expression
         return njs_parser_after(parser, current, node, 0,
                                 njs_parser_for_in_statement_statement);
     }
-
 }
 
 
@@ -5766,8 +5777,14 @@ njs_parser_iteration_statement_for_map(n
 
         *text = token->text;
 
+        ret = njs_parser_after(parser, current, text, 1,
+                               njs_parser_for_expression_map_continue);
+        if (ret != NJS_OK) {
+            return NJS_ERROR;
+        }
+
         return njs_parser_after(parser, current, text, 0,
-                                njs_parser_for_left_hand_side_expression_map);
+                                njs_parser_for_expression_map_reparse);
     }
 
 expression_after:
diff -r e74e56120102 -r b9d18d4dd34e src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri May 26 19:13:41 2023 -0700
+++ b/src/test/njs_unit_test.c	Fri May 26 19:43:24 2023 -0700
@@ -2963,6 +2963,12 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("for(A?{,"),
       njs_str("SyntaxError: Unexpected token \",\" in 1") },
 
+    { njs_str("for(Symbol(A=>A+       in 'A') P/$"),
+      njs_str("SyntaxError: Unexpected token \"in\" in 1") },
+
+    { njs_str("for (a(b * in d) ;"),
+      njs_str("SyntaxError: Unexpected token \"in\" in 1") },
+
     /* switch. */
 
     { njs_str("switch"),


More information about the nginx-devel mailing list