[llvm-branch-commits] [clang] 4b2f37b - [clang] fix undefined behaviour in RawComment::getFormattedText()

Tom Stellard via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon May 18 21:07:54 PDT 2020


Author: Oliver Bruns
Date: 2020-05-18T21:07:01-07:00
New Revision: 4b2f37b2202ebb5c6c05cf00026f506c52a62909

URL: https://github.com/llvm/llvm-project/commit/4b2f37b2202ebb5c6c05cf00026f506c52a62909
DIFF: https://github.com/llvm/llvm-project/commit/4b2f37b2202ebb5c6c05cf00026f506c52a62909.diff

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

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 ad7211df6f257e39da2e5a11b2456b4488f32a1e)

Added: 
    

Modified: 
    clang/lib/AST/RawCommentList.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/RawCommentList.cpp b/clang/lib/AST/RawCommentList.cpp
index 83e8a0b942a4..d7124156521c 100644
--- a/clang/lib/AST/RawCommentList.cpp
+++ b/clang/lib/AST/RawCommentList.cpp
@@ -430,7 +430,7 @@ std::string RawComment::getFormattedText(const SourceManager &SourceMgr,
   };
 
   auto DropTrailingNewLines = [](std::string &Str) {
-    while (Str.back() == '\n')
+    while (!Str.empty() && Str.back() == '\n')
       Str.pop_back();
   };
 


        


More information about the llvm-branch-commits mailing list