Skip to content

Commit 4b2f37b

Browse files
obrunststellar
authored andcommitted
[clang] fix undefined behaviour in RawComment::getFormattedText()
Summary: Calling `back()` and `pop_back()` on the empty string is undefined behavior [1,2]. The issue manifested itself as an uncaught `std::out_of_range` exception when running `clangd` compiled on RHEL7 using devtoolset-9. [1] https://en.cppreference.com/w/cpp/string/basic_string/back [2] https://en.cppreference.com/w/cpp/string/basic_string/pop_back Fixes: 1ff7c32 Reviewers: teemperor, ioeric, cfe-commits Reviewed By: teemperor Subscribers: ilya-biryukov, kadircet, usaxena95 Tags: #clang Differential Revision: https://reviews.llvm.org/D77468 (cherry picked from commit ad7211d)
1 parent d9160ff commit 4b2f37b

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

clang/lib/AST/RawCommentList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ std::string RawComment::getFormattedText(const SourceManager &SourceMgr,
430430
};
431431

432432
auto DropTrailingNewLines = [](std::string &Str) {
433-
while (Str.back() == '\n')
433+
while (!Str.empty() && Str.back() == '\n')
434434
Str.pop_back();
435435
};
436436

0 commit comments

Comments
 (0)