Skip to content

Commit 803883b

Browse files
committed
v8: fix template literal NULL pointer deref
Fixes a NULL pointer dereference with unterminated template literals. This is a back-port of commit v8/v8@02218ad from the V8 master branch, see https://code.google.com/p/v8/issues/detail?id=3820. PR-URL: #534 Reviewed-By: Caitlin Potter <caitpotter88@gmail.com> Reviewed-By: Fedor Indutny <fedor@indutny.com>
1 parent 5435cf2 commit 803883b

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

deps/v8/src/preparser.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,11 +2875,17 @@ ParserBase<Traits>::ParseTemplateLiteral(ExpressionT tag, int start, bool* ok) {
28752875

28762876
do {
28772877
next = peek();
2878-
if (!next) {
2878+
if (next == Token::EOS) {
28792879
ReportMessageAt(Scanner::Location(start, peek_position()),
28802880
"unterminated_template");
28812881
*ok = false;
28822882
return Traits::EmptyExpression();
2883+
} else if (next == Token::ILLEGAL) {
2884+
Traits::ReportMessageAt(
2885+
Scanner::Location(position() + 1, peek_position()),
2886+
"unexpected_token", "ILLEGAL", false);
2887+
*ok = false;
2888+
return Traits::EmptyExpression();
28832889
}
28842890

28852891
int expr_pos = peek_position();
@@ -2898,11 +2904,17 @@ ParserBase<Traits>::ParseTemplateLiteral(ExpressionT tag, int start, bool* ok) {
28982904
next = scanner()->ScanTemplateContinuation();
28992905
Next();
29002906

2901-
if (!next) {
2902-
ReportMessageAt(Scanner::Location(start, position()),
2907+
if (next == Token::EOS) {
2908+
ReportMessageAt(Scanner::Location(start, peek_position()),
29032909
"unterminated_template");
29042910
*ok = false;
29052911
return Traits::EmptyExpression();
2912+
} else if (next == Token::ILLEGAL) {
2913+
Traits::ReportMessageAt(
2914+
Scanner::Location(position() + 1, peek_position()),
2915+
"unexpected_token", "ILLEGAL", false);
2916+
*ok = false;
2917+
return Traits::EmptyExpression();
29062918
}
29072919

29082920
Traits::AddTemplateSpan(&ts, next == Token::TEMPLATE_TAIL);

deps/v8/test/cctest/test-parsing.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4549,6 +4549,28 @@ TEST(ScanUnterminatedTemplateLiterals) {
45494549
}
45504550

45514551

4552+
TEST(TemplateLiteralsIllegalTokens) {
4553+
const char* context_data[][2] = {{"'use strict';", ""},
4554+
{"function foo(){ 'use strict';"
4555+
" var a, b, c; return ", "}"},
4556+
{NULL, NULL}};
4557+
const char* data[] = {
4558+
"`hello\\x`",
4559+
"`hello\\x${1}`",
4560+
"`hello${1}\\x`",
4561+
"`hello${1}\\x${2}`",
4562+
"`hello\\x\n`",
4563+
"`hello\\x\n${1}`",
4564+
"`hello${1}\\x\n`",
4565+
"`hello${1}\\x\n${2}`",
4566+
NULL};
4567+
4568+
static const ParserFlag always_flags[] = {kAllowHarmonyTemplates};
4569+
RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
4570+
arraysize(always_flags));
4571+
}
4572+
4573+
45524574
TEST(LexicalScopingSloppyMode) {
45534575
const char* context_data[][2] = {
45544576
{"", ""},

0 commit comments

Comments
 (0)