[njs] Fixed parsing of string literals.

Dmitry Volyntsev xeioex at nginx.com
Thu Feb 14 11:41:43 UTC 2019


details:   https://hg.nginx.org/njs/rev/2a425a8ea90f
branches:  
changeset: 777:2a425a8ea90f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Feb 14 14:40:50 2019 +0300
description:
Fixed parsing of string literals.

This closes #93 issue on Github.

diffstat:

 njs/njs_lexer.c          |  11 +++++++++++
 njs/test/njs_unit_test.c |  35 +++++++++++++++++++++++++++++++----
 2 files changed, 42 insertions(+), 4 deletions(-)

diffs (106 lines):

diff -r 03be823cd95b -r 2a425a8ea90f njs/njs_lexer.c
--- a/njs/njs_lexer.c	Tue Feb 12 18:56:04 2019 +0300
+++ b/njs/njs_lexer.c	Thu Feb 14 14:40:50 2019 +0300
@@ -530,11 +530,22 @@ njs_lexer_string(njs_lexer_t *lexer, u_c
             }
 
             p++;
+
+            /* Line continuation. */
+            if (p < lexer->end && p[-1] == '\r' && p[0] == '\n') {
+                p++;
+            }
+
             escape = 1;
 
             continue;
         }
 
+        /* Line terminator. */
+        if (c == '\r' || c == '\n') {
+            break;
+        }
+
         if (c == quote) {
             lexer->start = p;
             lexer->text.length = (p - 1) - lexer->text.start;
diff -r 03be823cd95b -r 2a425a8ea90f njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Tue Feb 12 18:56:04 2019 +0300
+++ b/njs/test/njs_unit_test.c	Thu Feb 14 14:40:50 2019 +0300
@@ -2863,7 +2863,7 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("({[]:1})"),
       nxt_string("SyntaxError: Unexpected token \"[\" in 1") },
 
-    { nxt_string("({'AB\n\\cd':1})['AB\n\\cd']"),
+    { nxt_string("({'AB\\ncd':1})['AB\\ncd']"),
       nxt_string("1") },
 
     /* Inheritance. */
@@ -3935,6 +3935,15 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'\\a \\' \\\" \\\\ \\0 \\b \\f \\n \\r \\t \\v'"),
       nxt_string("a ' \" \\ \0 \b \f \n \r \t \v") },
 
+    { nxt_string("'\\\n'"),
+      nxt_string("") },
+
+    { nxt_string("'\\\r'"),
+      nxt_string("") },
+
+    { nxt_string("'\\\r\n'"),
+      nxt_string("") },
+
     { nxt_string("'a\\\nb'"),
       nxt_string("ab") },
 
@@ -3944,15 +3953,33 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("'a\\\r\nb'"),
       nxt_string("ab") },
 
+    { nxt_string("'a\\\n\rb'"),
+      nxt_string("SyntaxError: Unterminated string \"'a\\\n\r\" in 1") },
+
+    { nxt_string("'a\\\nb\nc'"),
+      nxt_string("SyntaxError: Unterminated string \"'a\\\nb\n\" in 1") },
+
     { nxt_string("'abcde"),
       nxt_string("SyntaxError: Unterminated string \"'abcde\" in 1") },
 
     { nxt_string("'\\"),
       nxt_string("SyntaxError: Unterminated string \"'\\\" in 1") },
 
+    { nxt_string("'\\\r\n"),
+      nxt_string("SyntaxError: Unterminated string \"'\\\r\n\" in 1") },
+
     { nxt_string("'\\'"),
       nxt_string("SyntaxError: Unterminated string \"'\\'\" in 1") },
 
+    { nxt_string("'a\n"),
+      nxt_string("SyntaxError: Unterminated string \"'a\n\" in 1") },
+
+    { nxt_string("'a\r"),
+      nxt_string("SyntaxError: Unterminated string \"'a\r\" in 1") },
+
+    { nxt_string("\"a\n"),
+      nxt_string("SyntaxError: Unterminated string \"\"a\n\" in 1") },
+
     { nxt_string("'\\u03B1'"),
       nxt_string("α") },
 
@@ -10464,14 +10491,14 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("JSON.stringify('α𐐀z'.repeat(10)).length"),
       nxt_string("32") },
 
-    { nxt_string("JSON.stringify('a\nbc')"),
+    { nxt_string("JSON.stringify('a\\nbc')"),
       nxt_string("\"a\\nbc\"") },
 
     { nxt_string("JSON.stringify('а\tбв')"),
       nxt_string("\"а\\tбв\"") },
 
-    { nxt_string("JSON.stringify('\n\t\r\"\f\b ')"),
-      nxt_string("\"\\n\\t\\r\\\"\\f\\b \"") },
+    { nxt_string("JSON.stringify('\\n\\t\\r\\\"\\f\\b')"),
+      nxt_string("\"\\n\\t\\r\\\"\\f\\b\"") },
 
     { nxt_string("JSON.stringify('\x00\x01\x02\x1f')"),
       nxt_string("\"\\u0000\\u0001\\u0002\\u001F\"") },


More information about the nginx-devel mailing list