Skip to content

Commit

Permalink
v8: fix template literal NULL pointer deref
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Fedor Indutny <[email protected]>
  • Loading branch information
bnoordhuis committed Jan 21, 2015
1 parent 5435cf2 commit 803883b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
18 changes: 15 additions & 3 deletions deps/v8/src/preparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2875,11 +2875,17 @@ ParserBase<Traits>::ParseTemplateLiteral(ExpressionT tag, int start, bool* ok) {

do {
next = peek();
if (!next) {
if (next == Token::EOS) {
ReportMessageAt(Scanner::Location(start, peek_position()),
"unterminated_template");
*ok = false;
return Traits::EmptyExpression();
} else if (next == Token::ILLEGAL) {
Traits::ReportMessageAt(
Scanner::Location(position() + 1, peek_position()),
"unexpected_token", "ILLEGAL", false);
*ok = false;
return Traits::EmptyExpression();
}

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

if (!next) {
ReportMessageAt(Scanner::Location(start, position()),
if (next == Token::EOS) {
ReportMessageAt(Scanner::Location(start, peek_position()),
"unterminated_template");
*ok = false;
return Traits::EmptyExpression();
} else if (next == Token::ILLEGAL) {
Traits::ReportMessageAt(
Scanner::Location(position() + 1, peek_position()),
"unexpected_token", "ILLEGAL", false);
*ok = false;
return Traits::EmptyExpression();
}

Traits::AddTemplateSpan(&ts, next == Token::TEMPLATE_TAIL);
Expand Down
22 changes: 22 additions & 0 deletions deps/v8/test/cctest/test-parsing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4549,6 +4549,28 @@ TEST(ScanUnterminatedTemplateLiterals) {
}


TEST(TemplateLiteralsIllegalTokens) {
const char* context_data[][2] = {{"'use strict';", ""},
{"function foo(){ 'use strict';"
" var a, b, c; return ", "}"},
{NULL, NULL}};
const char* data[] = {
"`hello\\x`",
"`hello\\x${1}`",
"`hello${1}\\x`",
"`hello${1}\\x${2}`",
"`hello\\x\n`",
"`hello\\x\n${1}`",
"`hello${1}\\x\n`",
"`hello${1}\\x\n${2}`",
NULL};

static const ParserFlag always_flags[] = {kAllowHarmonyTemplates};
RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
arraysize(always_flags));
}


TEST(LexicalScopingSloppyMode) {
const char* context_data[][2] = {
{"", ""},
Expand Down

0 comments on commit 803883b

Please sign in to comment.