Skip to content

Commit ad7211d

Browse files
obrunsTeemperor
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
1 parent 3c2dc28 commit ad7211d

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
@@ -431,7 +431,7 @@ std::string RawComment::getFormattedText(const SourceManager &SourceMgr,
431431
};
432432

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

0 commit comments

Comments
 (0)