Merging r342461:
authorTom Stellard <tstellar@redhat.com>
Mon, 22 Oct 2018 16:37:14 +0000 (16:37 +0000)
committerTom Stellard <tstellar@redhat.com>
Mon, 22 Oct 2018 16:37:14 +0000 (16:37 +0000)
------------------------------------------------------------------------
r342461 | devnexen | 2018-09-18 03:31:10 -0700 (Tue, 18 Sep 2018) | 10 lines

[Xray] llvm-xray fix possible segfault

top argument when superior to the instrumentated code list capacity can lead to a segfault.

Reviewers: dberris

Reviewed By: dberris

Differential Revision: https://reviews.llvm.org/D52224

------------------------------------------------------------------------

git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_70@344918 91177308-0d34-0410-b5e6-96231b3b80d8

tools/llvm-xray/xray-account.cpp

index 2776a88884811372f7298de261eaa3d0eeb5bac5..77de1de496f63e4554bde40a8be7ead31f0394f5 100644 (file)
@@ -358,8 +358,11 @@ void LatencyAccountant::exportStats(const XRayFileHeader &Header, F Fn) const {
     break;
   }
 
-  if (AccountTop > 0)
-    Results.erase(Results.begin() + AccountTop.getValue(), Results.end());
+  if (AccountTop > 0) {
+    auto MaxTop =
+        std::min(AccountTop.getValue(), static_cast<int>(Results.size()));
+    Results.erase(Results.begin() + MaxTop, Results.end());
+  }
 
   for (const auto &R : Results)
     Fn(std::get<0>(R), std::get<1>(R), std::get<2>(R));