[llvm] efa8374 - [llvm-cov] Use uint64_t to avoid overflow in addition

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 18 01:52:01 PDT 2023


Author: Flightor
Date: 2023-08-18T16:51:29+08:00
New Revision: efa8374f6b56dff31b81eb712c157bdc4f0cf830

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

LOG: [llvm-cov] Use uint64_t to avoid overflow in addition

Reviewed By: alanphipps
Differential Revision: https://reviews.llvm.org/D157608

Added: 
    

Modified: 
    llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
    llvm/tools/llvm-cov/SourceCoverageViewText.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
index 50e3dcc4db42d5..49f203507da0d7 100644
--- a/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
+++ b/llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
@@ -668,7 +668,9 @@ void SourceCoverageViewHTML::renderBranchView(raw_ostream &OS, BranchView &BRV,
     // Calculate TruePercent and False Percent.
     double TruePercent = 0.0;
     double FalsePercent = 0.0;
-    unsigned Total = R.ExecutionCount + R.FalseExecutionCount;
+    // FIXME: It may overflow when the data is too large, but I have not
+    // encountered it in actual use, and not sure whether to use __uint128_t.
+    uint64_t Total = R.ExecutionCount + R.FalseExecutionCount;
 
     if (!getOptions().ShowBranchCounts && Total != 0) {
       TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;

diff  --git a/llvm/tools/llvm-cov/SourceCoverageViewText.cpp b/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
index 40d4359e375c27..6e0db096a11c9f 100644
--- a/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
+++ b/llvm/tools/llvm-cov/SourceCoverageViewText.cpp
@@ -232,7 +232,9 @@ void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
   for (const auto &R : BRV.Regions) {
     double TruePercent = 0.0;
     double FalsePercent = 0.0;
-    unsigned Total = R.ExecutionCount + R.FalseExecutionCount;
+    // FIXME: It may overflow when the data is too large, but I have not
+    // encountered it in actual use, and not sure whether to use __uint128_t.
+    uint64_t Total = R.ExecutionCount + R.FalseExecutionCount;
 
     if (!getOptions().ShowBranchCounts && Total != 0) {
       TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;


        


More information about the llvm-commits mailing list