refi64 created this revision. Herald added subscribers: cfe-commits, usaxena95, kadircet, ilya-biryukov. Herald added a project: clang. refi64 added a reviewer: ilya-biryukov. refi64 added a comment.
(I manually ran clang-format-diff afterwards, there were no suggested changes.) If a string consisted only of whitespace, the emptiness check in getFormattedText would fail, but DropTrailingNewLines would then call Str.back() on an empty string. In practice, this can easily crash clangd if an editor asks for a declaration comment that is whitespace-only. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D77282 Files: clang/lib/AST/RawCommentList.cpp clang/unittests/AST/CommentTextTest.cpp Index: clang/unittests/AST/CommentTextTest.cpp =================================================================== --- clang/unittests/AST/CommentTextTest.cpp +++ clang/unittests/AST/CommentTextTest.cpp @@ -124,4 +124,10 @@ // clang-format on } +TEST_F(CommentTextTest, WorksWithEmptyBlockComments) { + auto ExpectedOutput = ""; + auto Formatted = formatComment(R"(/* */)"); + EXPECT_EQ(ExpectedOutput, Formatted); +} + } // namespace clang Index: clang/lib/AST/RawCommentList.cpp =================================================================== --- clang/lib/AST/RawCommentList.cpp +++ clang/lib/AST/RawCommentList.cpp @@ -431,7 +431,7 @@ }; auto DropTrailingNewLines = [](std::string &Str) { - while (Str.back() == '\n') + while (!Str.empty() && Str.back() == '\n') Str.pop_back(); };
Index: clang/unittests/AST/CommentTextTest.cpp =================================================================== --- clang/unittests/AST/CommentTextTest.cpp +++ clang/unittests/AST/CommentTextTest.cpp @@ -124,4 +124,10 @@ // clang-format on } +TEST_F(CommentTextTest, WorksWithEmptyBlockComments) { + auto ExpectedOutput = ""; + auto Formatted = formatComment(R"(/* */)"); + EXPECT_EQ(ExpectedOutput, Formatted); +} + } // namespace clang Index: clang/lib/AST/RawCommentList.cpp =================================================================== --- clang/lib/AST/RawCommentList.cpp +++ clang/lib/AST/RawCommentList.cpp @@ -431,7 +431,7 @@ }; auto DropTrailingNewLines = [](std::string &Str) { - while (Str.back() == '\n') + while (!Str.empty() && Str.back() == '\n') Str.pop_back(); };
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits