[clang] 882a05a - [Format] Fix crash when hitting eof while lexing JS template string
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 6 08:00:52 PDT 2022
Author: Sam McCall
Date: 2022-10-06T17:00:41+02:00
New Revision: 882a05afa17f2a8863978027f562934cd7a7d179
URL: https://github.com/llvm/llvm-project/commit/882a05afa17f2a8863978027f562934cd7a7d179
DIFF: https://github.com/llvm/llvm-project/commit/882a05afa17f2a8863978027f562934cd7a7d179.diff
LOG: [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
Added:
Modified:
clang/lib/Format/FormatTokenLexer.cpp
clang/unittests/Format/FormatTestJS.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp
index 313ea673ca757..f8f5f7112188c 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 9883aae62191a..f2a5559e871dc 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) {
More information about the cfe-commits
mailing list