[PATCH] D70200: Fix illegal cast from uint64_t to int64_t

Sajjad Mirza via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 13 12:18:40 PST 2019


sajjadm created this revision.
sajjadm added reviewers: Dor1s, vsk.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Counters are stored as uint64_t in the coverage mapping, but
exporting in JSON requires signed integers. Clamp the values to the
smaller range to make the conversion safe.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D70200

Files:
  llvm/tools/llvm-cov/CoverageExporterJson.cpp


Index: llvm/tools/llvm-cov/CoverageExporterJson.cpp
===================================================================
--- llvm/tools/llvm-cov/CoverageExporterJson.cpp
+++ llvm/tools/llvm-cov/CoverageExporterJson.cpp
@@ -48,6 +48,7 @@
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/Threading.h"
 #include <algorithm>
+#include <limits>
 #include <mutex>
 #include <utility>
 
@@ -61,14 +62,19 @@
 
 namespace {
 
+int64_t clamp_uint64_to_int64(uint64_t u) {
+  uint64_t int64_max = static_cast<uint64_t>(std::numeric_limits<int64_t>::max());
+  return u > int64_max ? int64_max : u;
+}
+
 json::Array renderSegment(const coverage::CoverageSegment &Segment) {
-  return json::Array({Segment.Line, Segment.Col, int64_t(Segment.Count),
+  return json::Array({Segment.Line, Segment.Col, clamp_uint64_to_int64(Segment.Count),
                       Segment.HasCount, Segment.IsRegionEntry});
 }
 
 json::Array renderRegion(const coverage::CountedRegion &Region) {
   return json::Array({Region.LineStart, Region.ColumnStart, Region.LineEnd,
-                      Region.ColumnEnd, int64_t(Region.ExecutionCount),
+                      Region.ColumnEnd, clamp_uint64_to_int64(Region.ExecutionCount),
                       Region.FileID, Region.ExpandedFileID,
                       int64_t(Region.Kind)});
 }
@@ -182,7 +188,7 @@
   for (const auto &F : Functions)
     FunctionArray.push_back(
         json::Object({{"name", F.Name},
-                      {"count", int64_t(F.ExecutionCount)},
+                      {"count", clamp_uint64_to_int64(F.ExecutionCount)},
                       {"regions", renderRegions(F.CountedRegions)},
                       {"filenames", json::Array(F.Filenames)}}));
   return FunctionArray;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70200.229150.patch
Type: text/x-patch
Size: 1746 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191113/2c597cb0/attachment.bin>


More information about the llvm-commits mailing list