From c19e57ee82a03e4b0aefa160a7b47443b528a91d Mon Sep 17 00:00:00 2001 From: Shu-yu Guo Date: Mon, 3 Aug 2020 10:20:36 -0700 Subject: [PATCH] Disallow \8 and \9 in strict mode and template literals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reached consensus in the July 2020 TC39: https://github.com/tc39/ecma262/pull/2054 Bug: v8:10769 Change-Id: Iecea1d9d9c9be5c2fbfb820aed2285719c4e6382 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2333350 Commit-Queue: Shu-yu Guo Reviewed-by: Marja Hölttä Reviewed-by: Leszek Swirski Cr-Commit-Position: refs/heads/master@{#69206} --- src/common/message-template.h | 2 ++ src/parsing/scanner.cc | 22 +++++++++++++------ test/mjsunit/es6/templates.js | 6 ++--- test/test262/test262.status | 4 ---- ...ic-escapes-in-string-literals-expected.txt | 7 +++--- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/common/message-template.h b/src/common/message-template.h index cf8d66b8bb60..b180a752c7ae 100644 --- a/src/common/message-template.h +++ b/src/common/message-template.h @@ -482,9 +482,11 @@ namespace internal { "Decimals with leading zeros are not allowed in strict mode.") \ T(StrictOctalEscape, \ "Octal escape sequences are not allowed in strict mode.") \ + T(Strict8Or9Escape, "\\8 and \\9 are not allowed in strict mode.") \ T(StrictWith, "Strict mode code may not include a with statement") \ T(TemplateOctalLiteral, \ "Octal escape sequences are not allowed in template strings.") \ + T(Template8Or9Escape, "\\8 and \\9 are not allowed in template strings.") \ T(ThisFormalParameter, "'this' is not a valid formal parameter name") \ T(AwaitBindingIdentifier, \ "'await' is not a valid identifier name in an async function") \ diff --git a/src/parsing/scanner.cc b/src/parsing/scanner.cc index e27cb041020b..9d2da41525c2 100644 --- a/src/parsing/scanner.cc +++ b/src/parsing/scanner.cc @@ -403,16 +403,24 @@ bool Scanner::ScanEscape() { if (IsInvalid(c)) return false; break; } - case '0': // Fall through. - case '1': // fall through - case '2': // fall through - case '3': // fall through - case '4': // fall through - case '5': // fall through - case '6': // fall through + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': case '7': c = ScanOctalEscape(c, 2); break; + case '8': + case '9': + // '\8' and '\9' are disallowed in strict mode. + // Re-use the octal error state to propagate the error. + octal_pos_ = Location(source_pos() - 2, source_pos() - 1); + octal_message_ = capture_raw ? MessageTemplate::kTemplate8Or9Escape + : MessageTemplate::kStrict8Or9Escape; + break; } // Other escaped characters are interpreted as their non-escaped version. diff --git a/test/mjsunit/es6/templates.js b/test/mjsunit/es6/templates.js index 3da06b2b0f05..2b0a5cc12f9d 100644 --- a/test/mjsunit/es6/templates.js +++ b/test/mjsunit/es6/templates.js @@ -506,9 +506,9 @@ var obj = { })(); -(function testValidNumericEscapes() { - assertEquals("8", `\8`); - assertEquals("9", `\9`); +(function testInvalidNumericEscapes() { + assertThrows(function() { eval("`\\8`"); }, SyntaxError) + assertThrows(function() { eval("`\\9`"); }, SyntaxError) })(); diff --git a/test/test262/test262.status b/test/test262/test262.status index dded88081c4c..8dbfcbeb4876 100644 --- a/test/test262/test262.status +++ b/test/test262/test262.status @@ -554,10 +554,6 @@ 'built-ins/Atomics/xor/bigint/non-shared-bufferdata': [FAIL], 'built-ins/Atomics/xor/non-shared-bufferdata': [FAIL], - # https://bugs.chromium.org/p/v8/issues/detail?id=10769 - 'language/expressions/template-literal/invalid-legacy-octal-escape-sequence-8': [FAIL], - 'language/literals/string/legacy-non-octal-escape-sequence-8-strict': [FAIL], - ######################## NEEDS INVESTIGATION ########################### # https://bugs.chromium.org/p/v8/issues/detail?id=7833 diff --git a/test/webkit/fast/js/numeric-escapes-in-string-literals-expected.txt b/test/webkit/fast/js/numeric-escapes-in-string-literals-expected.txt index 718e718a0de9..f69d01e1459d 100644 --- a/test/webkit/fast/js/numeric-escapes-in-string-literals-expected.txt +++ b/test/webkit/fast/js/numeric-escapes-in-string-literals-expected.txt @@ -25,7 +25,6 @@ Test numeric escapes in string literals - https://bugs.webkit.org/show_bug.cgi?i On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE". - PASS eval(stringLiteral) is nonStrictResult PASS eval(stringLiteral) is strictResult PASS eval(stringLiteral) is nonStrictResult @@ -33,9 +32,9 @@ PASS eval(stringLiteral) threw exception SyntaxError: Octal escape sequences are PASS eval(stringLiteral) is nonStrictResult PASS eval(stringLiteral) threw exception SyntaxError: Octal escape sequences are not allowed in strict mode.. PASS eval(stringLiteral) is nonStrictResult -FAIL eval(stringLiteral) should throw an exception. Was 8. +PASS eval(stringLiteral) threw exception SyntaxError: \8 and \9 are not allowed in strict mode.. PASS eval(stringLiteral) is nonStrictResult -FAIL eval(stringLiteral) should throw an exception. Was 9. +PASS eval(stringLiteral) threw exception SyntaxError: \8 and \9 are not allowed in strict mode.. PASS eval(stringLiteral) is nonStrictResult PASS eval(stringLiteral) threw exception SyntaxError: Octal escape sequences are not allowed in strict mode.. PASS eval(stringLiteral) is nonStrictResult @@ -61,7 +60,7 @@ PASS eval(stringLiteral) threw exception SyntaxError: Octal escape sequences are PASS eval(stringLiteral) is nonStrictResult PASS eval(stringLiteral) threw exception SyntaxError: Octal escape sequences are not allowed in strict mode.. PASS eval(stringLiteral) is nonStrictResult -FAIL eval(stringLiteral) should throw an exception. Was 99. +PASS eval(stringLiteral) threw exception SyntaxError: \8 and \9 are not allowed in strict mode.. PASS successfullyParsed is true TEST COMPLETE