[llvm] [llvm] tighten estimate of bits needed when parsing an integer (PR #205947)

Jeremy Kun via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 26 13:04:29 PDT 2026


================
@@ -522,12 +522,35 @@ bool StringRef::consumeInteger(unsigned Radix, APInt &Result) {
     return false;
   }
 
-  // (Over-)estimate the required number of bits.
+  // The string is too long to be reasonable
+  if (Str.size() > std::numeric_limits<unsigned>::max())
----------------
j2kun wrote:

Excuse my naivete here, but your comment implies the original code (repeated below) was a bug, right?

```cpp
unsigned BitWidth = Log2Radix * Str.size();
``

If so, then I see two ways forward:

1. Instead guard directly in the overflow of the EstimatedBitWidth below
2. Scan forward to determine the total number of digits needed

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


More information about the llvm-commits mailing list