From: Vadim Zhestikov Date: Mon, 11 Jul 2022 14:25:03 +0000 (-0700) Subject: Throwing SyntaxError for octal escape sequences and \8 and \9. X-Git-Tag: 0.7.6~7 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=7f633259fc546c5c97621030f1cfc6a7bb54eb7e;p=njs.git Throwing SyntaxError for octal escape sequences and \8 and \9. Error messages are same as messages generated by FireFox, because they are looks more informative in comparison with messages generated by chrome, nodejs, quickjs. --- diff --git a/src/njs_parser.c b/src/njs_parser.c index 92a05a42..2e09f870 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -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; diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 8e995bf5..39345a01 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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'"),