[njs] Lexer: fixed handling of no-newline at the end of the script.

Dmitry Volyntsev xeioex at nginx.com
Mon Mar 23 14:16:07 UTC 2020


details:   https://hg.nginx.org/njs/rev/603a21fabf8d
branches:  
changeset: 1361:603a21fabf8d
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Mar 23 17:15:46 2020 +0300
description:
Lexer: fixed handling of no-newline at the end of the script.

diffstat:

 src/njs_lexer.c          |   4 +---
 src/njs_shell.c          |  13 ++++++++++---
 src/test/njs_unit_test.c |  26 ++++++++++++++++++--------
 3 files changed, 29 insertions(+), 14 deletions(-)

diffs (106 lines):

diff -r 9b20edb591c4 -r 603a21fabf8d src/njs_lexer.c
--- a/src/njs_lexer.c	Fri Mar 20 17:33:10 2020 +0300
+++ b/src/njs_lexer.c	Mon Mar 23 17:15:46 2020 +0300
@@ -903,13 +903,11 @@ njs_lexer_division(njs_lexer_t *lexer, n
     c = lexer->start[0];
 
     if (c == '/') {
-        token->type = NJS_TOKEN_END;
-
         lexer->start++;
 
         for (p = lexer->start; p < lexer->end; p++) {
 
-            if (*p == '\n') {
+            if (*p == '\n' || (p + 1) == lexer->end) {
                 lexer->start = p + 1;
                 lexer->line++;
 
diff -r 9b20edb591c4 -r 603a21fabf8d src/njs_shell.c
--- a/src/njs_shell.c	Fri Mar 20 17:33:10 2020 +0300
+++ b/src/njs_shell.c	Mon Mar 23 17:15:46 2020 +0300
@@ -801,17 +801,24 @@ static njs_int_t
 njs_process_script(njs_opts_t *opts, njs_console_t *console,
     const njs_str_t *script)
 {
-    u_char     *start;
+    u_char     *start, *end;
     njs_vm_t   *vm;
     njs_int_t  ret;
 
     vm = console->vm;
     start = script->start;
+    end = start + script->length;
 
-    ret = njs_vm_compile(vm, &start, start + script->length);
+    ret = njs_vm_compile(vm, &start, end);
 
     if (ret == NJS_OK) {
-        ret = njs_vm_start(vm);
+        if (start == end) {
+            ret = njs_vm_start(vm);
+
+        } else {
+            njs_vm_error(vm, "Extra characters at the end of the script");
+            ret = NJS_ERROR;
+        }
     }
 
     njs_output(opts, vm, ret);
diff -r 9b20edb591c4 -r 603a21fabf8d src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Mar 20 17:33:10 2020 +0300
+++ b/src/test/njs_unit_test.c	Mon Mar 23 17:15:46 2020 +0300
@@ -35,6 +35,9 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("/***/1/*\n**/"),
       njs_str("1") },
 
+    { njs_str("/***/1//  "),
+      njs_str("1") },
+
     { njs_str(">"),
       njs_str("SyntaxError: Unexpected token \">\" in 1") },
 
@@ -17080,7 +17083,7 @@ static njs_int_t
 njs_unit_test(njs_unit_test_t tests[], size_t num, const char *name,
     njs_opts_t *opts, njs_stat_t *stat)
 {
-    u_char                *start;
+    u_char                *start, *end;
     njs_vm_t              *vm, *nvm;
     njs_int_t             ret;
     njs_str_t             s;
@@ -17123,10 +17126,11 @@ njs_unit_test(njs_unit_test_t tests[], s
         }
 
         start = tests[i].script.start;
-
-        ret = njs_vm_compile(vm, &start, start + tests[i].script.length);
-
-        if (ret == NJS_OK) {
+        end = start + tests[i].script.length;
+
+        ret = njs_vm_compile(vm, &start, end);
+
+        if (ret == NJS_OK && start == end) {
             if (opts->disassemble) {
                 njs_disassembler(vm);
             }
@@ -17160,9 +17164,15 @@ njs_unit_test(njs_unit_test_t tests[], s
             }
 
         } else {
-            if (njs_vm_retval_string(vm, &s) != NJS_OK) {
-                njs_printf("njs_vm_retval_string() failed\n");
-                goto done;
+            if (ret != NJS_OK) {
+                if (njs_vm_retval_string(vm, &s) != NJS_OK) {
+                    njs_printf("njs_vm_retval_string() failed\n");
+                    goto done;
+                }
+
+            } else {
+                s = njs_str_value("Error: "
+                                  "Extra characters at the end of the script");
             }
         }
 


More information about the nginx-devel mailing list