[clang] [analyzer] Fix truncation of ranges wider than 64 bits (PR #239147)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 22:15:49 PST 2026


================
@@ -715,25 +715,35 @@ RangeSet::ContainerType RangeSet::Factory::truncateTo(RangeSet What,
   using llvm::APSInt;
   ContainerType Result;
   ContainerType Dummy;
-  // CastRangeSize is an amount of all possible values of cast type.
-  // Example: `char` has 256 values; `short` has 65536 values.
-  // But in fact we use `amount of values` - 1, because
-  // we can't keep `amount of values of UINT64` inside uint64_t.
-  // E.g. 256 is an amount of all possible values of `char` and we can't keep
-  // it inside `char`.
-  // And it's OK, it's enough to do correct calculations.
-  uint64_t CastRangeSize = APInt::getMaxValue(Ty.getBitWidth()).getZExtValue();
+  // Compare cardinalities minus one at source width to avoid overflow.
+  //
+  // Both comparison operands can need more than 64 bits:
+  // - A source span can be as large as `2^W - 1`.
+  // - A target wider than 64 bits has a threshold that does not fit in
+  // `uint64_t`.
+  //
+  // Keep them in `APInt` so the comparison below uses the same width on both
+  // sides.
+  const APInt CastRangeSize =
----------------
relaxcn wrote:

Make sense. Done.

https://github.com/llvm/llvm-project/pull/239147


More information about the cfe-commits mailing list