[njs] Fixed handling of unexpected tokens in parser.

Dmitry Volyntsev xeioex at nginx.com
Thu Nov 22 12:54:42 UTC 2018


details:   https://hg.nginx.org/njs/rev/01f2cbf5acc4
branches:  
changeset: 662:01f2cbf5acc4
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Nov 22 15:02:19 2018 +0300
description:
Fixed handling of unexpected tokens in parser.

This fixes #50 issue on Github.

diffstat:

 njs/njs.c                       |  2 ++
 njs/njs_parser.c                |  8 ++++----
 njs/test/njs_interactive_test.c |  4 ++++
 njs/test/njs_unit_test.c        |  3 +++
 4 files changed, 13 insertions(+), 4 deletions(-)

diffs (78 lines):

diff -r db44b92af5f7 -r 01f2cbf5acc4 njs/njs.c
--- a/njs/njs.c	Wed Nov 21 16:45:10 2018 +0300
+++ b/njs/njs.c	Thu Nov 22 15:02:19 2018 +0300
@@ -252,6 +252,8 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
         nxt_array_reset(vm->backtrace);
     }
 
+    vm->retval = njs_value_void;
+
     node = njs_parser(vm, parser, prev);
     if (nxt_slow_path(node == NULL)) {
         goto fail;
diff -r db44b92af5f7 -r 01f2cbf5acc4 njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Nov 21 16:45:10 2018 +0300
+++ b/njs/njs_parser.c	Thu Nov 22 15:02:19 2018 +0300
@@ -639,7 +639,7 @@ njs_parser_function_lambda(njs_vm_t *vm,
     while (token != NJS_TOKEN_CLOSE_PARENTHESIS) {
 
         if (nxt_slow_path(token != NJS_TOKEN_NAME)) {
-            return NJS_TOKEN_ERROR;
+            return NJS_TOKEN_ILLEGAL;
         }
 
         arg = njs_variable_add(vm, parser, NJS_VARIABLE_VAR);
@@ -676,7 +676,7 @@ njs_parser_function_lambda(njs_vm_t *vm,
     }
 
     if (nxt_slow_path(token != NJS_TOKEN_OPEN_BRACE)) {
-        return NJS_TOKEN_ERROR;
+        return NJS_TOKEN_ILLEGAL;
     }
 
     token = njs_parser_token(parser);
@@ -1649,7 +1649,7 @@ njs_parser_try_statement(njs_vm_t *vm, n
         }
 
         if (nxt_slow_path(token != NJS_TOKEN_CLOSE_PARENTHESIS)) {
-            return token;
+            return NJS_TOKEN_ILLEGAL;
         }
 
         token = njs_parser_try_block(vm, parser);
@@ -1689,7 +1689,7 @@ njs_parser_try_statement(njs_vm_t *vm, n
 
     if (try->right == NULL) {
         njs_parser_syntax_error(vm, parser,
-                                "Missing catch or finally after try", NULL);
+                                "Missing catch or finally after try");
 
         return NJS_TOKEN_ILLEGAL;
     }
diff -r db44b92af5f7 -r 01f2cbf5acc4 njs/test/njs_interactive_test.c
--- a/njs/test/njs_interactive_test.c	Wed Nov 21 16:45:10 2018 +0300
+++ b/njs/test/njs_interactive_test.c	Thu Nov 22 15:02:19 2018 +0300
@@ -106,6 +106,10 @@ static njs_interactive_test_t  njs_test[
                  "f({})" ENTER),
       nxt_string("1") },
 
+    { nxt_string("arguments" ENTER
+                 "function(){}()" ENTER),
+      nxt_string("SyntaxError: Unexpected token \"(\" in 1") },
+
     /* Backtraces */
 
     { nxt_string("function ff(o) {return o.a.a}" ENTER
diff -r db44b92af5f7 -r 01f2cbf5acc4 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Wed Nov 21 16:45:10 2018 +0300
+++ b/njs/test/njs_unit_test.c	Thu Nov 22 15:02:19 2018 +0300
@@ -6356,6 +6356,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("try {}"),
       nxt_string("SyntaxError: Missing catch or finally after try in 1") },
 
+    { nxt_string("try{}catch(a[]"),
+      nxt_string("SyntaxError: Unexpected token \"[\" in 1") },
+
     { nxt_string("function f(a) {return a;}; "
                  "function thrower() {throw TypeError('Oops')}; "
                  "f(thrower())"),


More information about the nginx-devel mailing list