]> git.kaiwu.me - njs.git/commitdiff
Throwing SyntaxError for octal escape sequences and \8 and \9.
authorVadim Zhestikov <v.zhestikov@f5.com>
Mon, 11 Jul 2022 14:25:03 +0000 (07:25 -0700)
committerVadim Zhestikov <v.zhestikov@f5.com>
Mon, 11 Jul 2022 14:25:03 +0000 (07:25 -0700)
Error messages are same as messages generated by FireFox, because
they are looks more informative in comparison with messages
generated by chrome, nodejs, quickjs.

src/njs_parser.c
src/test/njs_unit_test.c

index 92a05a428a1d14e0241edfadbbdf1dd9d48bbe88..2e09f87006160676d4f5e7bbe14c77e5cf5a3feb 100644 (file)
@@ -8402,6 +8402,59 @@ njs_parser_escape_string_create(njs_parser_t *parser, njs_lexer_token_t *token,
                 c = '\0';
                 break;
 
+            case '1':
+            case '2':
+            case '3':
+            case '4':
+            case '5':
+            case '6':
+            case '7':
+                if (parser->node != NULL) {
+                    switch (parser->node->token_type) {
+                    case NJS_TOKEN_METHOD_CALL:
+                    case NJS_TOKEN_FUNCTION_CALL:
+                    case NJS_TOKEN_FUNCTION_EXPRESSION:
+                    case NJS_TOKEN_EVAL:
+                        goto next_char;
+
+                    default:
+                        break;
+                    }
+                }
+
+                njs_parser_syntax_error(parser,
+                                        "Octal escape sequences can't be used "
+                                        "in untagged template literals "
+                                        "or in strict mode code");
+
+                return NJS_TOKEN_ILLEGAL;
+
+            case '8':
+            case '9':
+                if (parser->node != NULL) {
+                    switch (parser->node->token_type) {
+                    case NJS_TOKEN_METHOD_CALL:
+                    case NJS_TOKEN_FUNCTION_CALL:
+                    case NJS_TOKEN_FUNCTION_EXPRESSION:
+                    case NJS_TOKEN_EVAL:
+                        goto next_char;
+
+                    default:
+                        break;
+                    }
+                }
+
+                njs_parser_syntax_error(parser,
+                                        "The escapes \\8 and \\9 can't be used "
+                                        "in untagged template literals "
+                                        "or in strict mode code");
+
+                return NJS_TOKEN_ILLEGAL;
+
+next_char:
+
+                break;
+
             case 'b':
                 c = '\b';
                 break;
index 8e995bf590be5d1721f6996134aabff21da72186..39345a01c750e22290a3b398f89b80e5f57b0b2d 100644 (file)
@@ -7457,6 +7457,66 @@ static njs_unit_test_t  njs_test[] =
     { njs_str("'r' !== '\\r'"),
       njs_str("true") },
 
+    /* Octal escape sequences are not allowed in strict mode.*/
+
+    { njs_str("'\\0a'"),
+      njs_str("\0a") },
+
+    { njs_str("'\\1a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'a\\2a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'\\3a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'a\\4a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'\\5a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'a\\6a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'\\7a'"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'\\8a'"),
+      njs_str("SyntaxError: The escapes \\8 and \\9 can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'\\9a'"),
+      njs_str("SyntaxError: The escapes \\8 and \\9 can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("'\\aa'"),
+      njs_str("aa") },
+
+    { njs_str("'\\*a'"),
+      njs_str("*a") },
+
+    { njs_str("`\\7`"),
+      njs_str("SyntaxError: Octal escape sequences can't be used in untagged template literals or in strict mode code in 1") },
+
+    { njs_str("`\\9`"),
+      njs_str("SyntaxError: The escapes \\8 and \\9 can't be used in untagged template literals or in strict mode code in 1") },
+
+    /* Octal escape sequences are allowed in tagged template literals in strict mode.*/
+
+#if 0  /* FIXME: tag function runtime semantics */
+    { njs_str("function x (s) {return s[0]}; x`\\7`"),
+      njs_str("undefined") },
+
+    { njs_str("function x (s) {return s[0]}; x`\\9`"),
+      njs_str("undefined") },
+
+    { njs_str("function x (s) {return s.raw[0]}; x`\\9`"),
+      njs_str("\\9") },
+
+    { njs_str("function x (s) {return s.raw[0]}; x`\\7`"),
+      njs_str("\\7") },
+#endif
+
     /* Broken UTF-8 literals.*/
 
     { njs_str("'\\a\x96\xE5\x9C\xE3\x81\xB6'"),