From 882a05afa17f2a8863978027f562934cd7a7d179 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Thu, 6 Oct 2022 13:10:19 +0200 Subject: [PATCH] [Format] Fix crash when hitting eof while lexing JS template string Different loop termination conditions resulted in confusion of whether *Offset was intended to be inside or outside the token. This ultimately led to constructing an out-of-range SourceLocation. Fix by making Offset consistently point *after* the token. Differential Revision: https://reviews.llvm.org/D135356 --- clang/lib/Format/FormatTokenLexer.cpp | 9 ++++----- clang/unittests/Format/FormatTestJS.cpp | 2 ++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 313ea67..f8f5f71 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -760,6 +760,7 @@ void FormatTokenLexer::handleTemplateStrings() { for (; Offset != Lex->getBuffer().end(); ++Offset) { if (Offset[0] == '`') { StateStack.pop(); + ++Offset; break; } if (Offset[0] == '\\') { @@ -768,12 +769,12 @@ void FormatTokenLexer::handleTemplateStrings() { Offset[1] == '{') { // '${' introduces an expression interpolation in the template string. StateStack.push(LexerState::NORMAL); - ++Offset; + Offset += 2; break; } } - StringRef LiteralText(TmplBegin, Offset - TmplBegin + 1); + StringRef LiteralText(TmplBegin, Offset - TmplBegin); BacktickToken->setType(TT_TemplateString); BacktickToken->Tok.setKind(tok::string_literal); BacktickToken->TokenText = LiteralText; @@ -794,9 +795,7 @@ void FormatTokenLexer::handleTemplateStrings() { StartColumn, Style.TabWidth, Encoding); } - SourceLocation loc = Offset < Lex->getBuffer().end() - ? Lex->getSourceLocation(Offset + 1) - : SourceMgr.getLocForEndOfFile(ID); + SourceLocation loc = Lex->getSourceLocation(Offset); resetLexer(SourceMgr.getFileOffset(loc)); } diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 9883aae..f2a5559 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -2145,6 +2145,8 @@ TEST_F(FormatTestJS, NestedTemplateStrings) { // Crashed at some point. verifyFormat("}"); + verifyFormat("`"); + verifyFormat("`\\"); } TEST_F(FormatTestJS, TaggedTemplateStrings) { -- 2.7.4