[PATCH] D77468: [clang] fix undefined behaviour in RawComment::getFormattedText()

Oliver Bruns via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 4 11:09:04 PDT 2020


obruns created this revision.
obruns added reviewers: teemperor, ioeric, cfe-commits.
Herald added subscribers: usaxena95, kadircet, ilya-biryukov.
Herald added a project: clang.

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: 1ff7c32fc91c607b690d4bb9cf42f406be8dde68


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77468

Files:
  clang/lib/AST/RawCommentList.cpp


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 (not Str.empty() and Str.back() == '\n')
       Str.pop_back();
   };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77468.255059.patch
Type: text/x-patch
Size: 379 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200404/5b737733/attachment-0001.bin>


More information about the cfe-commits mailing list