Author: Manuel Klimek Date: 2021-12-01T11:57:41+01:00 New Revision: d688b31628004ed9e109e63c2b43701f97252ff3
URL: https://github.com/llvm/llvm-project/commit/d688b31628004ed9e109e63c2b43701f97252ff3 DIFF: https://github.com/llvm/llvm-project/commit/d688b31628004ed9e109e63c2b43701f97252ff3.diff LOG: Fix segfault in clang-format. Fix bug where we'd read past the end of the tokens after merging _T macro strings. Added: Modified: clang/lib/Format/FormatTokenLexer.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 972f59ead5d8f..64fbd2d5d45b1 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -752,6 +752,8 @@ bool FormatTokenLexer::tryMerge_TMacro() { Tokens.pop_back(); Tokens.pop_back(); Tokens.back() = String; + if (FirstInLineIndex >= Tokens.size()) + FirstInLineIndex = Tokens.size() - 1; return true; } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 65809ab5e7579..9b6607b46d93a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -12879,6 +12879,12 @@ TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { format("f(\n" "\n" "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); + // Regression test for accessing tokens past the end of a vector in the + // TokenLexer. + verifyNoCrash(R"(_T( +" +) +)"); } TEST_F(FormatTest, BreaksStringLiteralOperands) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits