sergesanspaille / rpms / clang

Forked from rpms/clang 5 years ago
Clone
d933922
From ad7211df6f257e39da2e5a11b2456b4488f32a1e Mon Sep 17 00:00:00 2001
d933922
From: Oliver Bruns <obruns@gmail.com>
d933922
Date: Mon, 6 Apr 2020 10:38:30 +0200
d933922
Subject: [PATCH] [clang] fix undefined behaviour in
d933922
 RawComment::getFormattedText()
d933922
d933922
Summary:
d933922
Calling `back()` and `pop_back()` on the empty string is undefined
d933922
behavior [1,2].
d933922
d933922
The issue manifested itself as an uncaught `std::out_of_range` exception
d933922
when running `clangd` compiled on RHEL7 using devtoolset-9.
d933922
d933922
[1] https://en.cppreference.com/w/cpp/string/basic_string/back
d933922
[2] https://en.cppreference.com/w/cpp/string/basic_string/pop_back
d933922
d933922
Fixes: 1ff7c32fc91c607b690d4bb9cf42f406be8dde68
d933922
d933922
Reviewers: teemperor, ioeric, cfe-commits
d933922
d933922
Reviewed By: teemperor
d933922
d933922
Subscribers: ilya-biryukov, kadircet, usaxena95
d933922
d933922
Tags: #clang
d933922
d933922
Differential Revision: https://reviews.llvm.org/D77468
d933922
---
d933922
 clang/lib/AST/RawCommentList.cpp | 2 +-
d933922
 1 file changed, 1 insertion(+), 1 deletion(-)
d933922
d933922
diff --git a/clang/lib/AST/RawCommentList.cpp b/clang/lib/AST/RawCommentList.cpp
d933922
index 8552b4fcd2b..a8d15036cab 100644
d933922
--- a/clang/lib/AST/RawCommentList.cpp
d933922
+++ b/clang/lib/AST/RawCommentList.cpp
d933922
@@ -431,7 +431,7 @@ std::string RawComment::getFormattedText(const SourceManager &SourceMgr,
d933922
   };
d933922
 
d933922
   auto DropTrailingNewLines = [](std::string &Str) {
d933922
-    while (Str.back() == '\n')
d933922
+    while (!Str.empty() && Str.back() == '\n')
d933922
       Str.pop_back();
d933922
   };
d933922
 
d933922
-- 
d933922
2.25.2
d933922