[njs] Fixed parsing case/default in an unclosed function block in switch.

Alexander Borisov alexander.borisov at nginx.com
Mon May 17 17:36:36 UTC 2021


details:   https://hg.nginx.org/njs/rev/7b70ee90ead8
branches:  
changeset: 1637:7b70ee90ead8
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Mon May 17 20:35:35 2021 +0300
description:
Fixed parsing case/default in an unclosed function block in switch.

diffstat:

 src/njs_parser.c         |  13 ++++++++++++-
 src/test/njs_unit_test.c |  18 ++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletions(-)

diffs (65 lines):

diff -r 637bc81166a6 -r 7b70ee90ead8 src/njs_parser.c
--- a/src/njs_parser.c	Thu May 06 16:07:09 2021 +0300
+++ b/src/njs_parser.c	Mon May 17 20:35:35 2021 +0300
@@ -4713,7 +4713,10 @@ njs_parser_statement_list_next(njs_parse
     if (parser->ret != NJS_OK) {
         if (token->type != NJS_TOKEN_CLOSE_BRACE) {
             parser->node = parser->target;
-            return njs_parser_stack_pop(parser);
+
+            (void) njs_parser_stack_pop(parser);
+
+            return parser->ret;
         }
 
         return njs_parser_failed(parser);
@@ -6098,6 +6101,10 @@ njs_parser_switch_case_block(njs_parser_
 {
     parser->target->right->right = parser->node;
 
+    if (parser->ret != NJS_OK && parser->target->scope != parser->scope) {
+        return njs_parser_failed(parser);
+    }
+
     njs_parser_next(parser, njs_parser_switch_case);
 
     return NJS_OK;
@@ -6153,6 +6160,10 @@ njs_parser_switch_case_block_wo_def(njs_
         parser->target->right = parser->node;
     }
 
+    if (parser->ret != NJS_OK && parser->target->scope != parser->scope) {
+        return njs_parser_failed(parser);
+    }
+
     njs_parser_next(parser, njs_parser_switch_case_wo_def);
 
     return NJS_OK;
diff -r 637bc81166a6 -r 7b70ee90ead8 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Thu May 06 16:07:09 2021 +0300
+++ b/src/test/njs_unit_test.c	Mon May 17 20:35:35 2021 +0300
@@ -2958,6 +2958,24 @@ static njs_unit_test_t  njs_test[] =
               "map((v)=>{switch(v) { case isNaN: return 1; default: return 0;}})"),
       njs_str("1,0,0") },
 
+    { njs_str("switch (1) {case 1: ii > 1; ii => default:}"),
+      njs_str("SyntaxError: Unexpected token \"default\" in 1") },
+
+    { njs_str("switch (1) {case 1: ii > 1; var a = functin () {default:}"),
+      njs_str("SyntaxError: Unexpected token \"{\" in 1") },
+
+    { njs_str("switch (1) {default: ii > 1; ii => case 2:}"),
+      njs_str("SyntaxError: Unexpected token \"case\" in 1") },
+
+    { njs_str("switch (1) {default: ii > 1; var a = functin () {case 2:}"),
+      njs_str("SyntaxError: Unexpected token \"{\" in 1") },
+
+    { njs_str("switch (1) {case 1: ii > 1; ii => case 2:}"),
+      njs_str("SyntaxError: Unexpected token \"case\" in 1") },
+
+    { njs_str("switch (1) {case 1: ii > 1; var a = functin () {case 2:}"),
+      njs_str("SyntaxError: Unexpected token \"{\" in 1") },
+
     /* continue. */
 
     { njs_str("continue"),


More information about the nginx-devel mailing list