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

Jiang Haibo via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 10 05:55:35 PDT 2023


Flightor created this revision.
Flightor added reviewers: vsk, alanphipps.
Herald added a project: All.
Flightor requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

https://reviews.llvm.org/D157608

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


Index: llvm/tools/llvm-cov/SourceCoverageViewText.cpp
===================================================================
--- llvm/tools/llvm-cov/SourceCoverageViewText.cpp
+++ llvm/tools/llvm-cov/SourceCoverageViewText.cpp
@@ -232,7 +232,9 @@
   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;
Index: llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
===================================================================
--- llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
+++ llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
@@ -668,7 +668,9 @@
     // 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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157608.548998.patch
Type: text/x-patch
Size: 1532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230810/1a2c8c7d/attachment.bin>


More information about the llvm-commits mailing list