[flang-commits] [flang] [flang] Do not invoke undefined behaviour when parsing format expressions (PR #147539)

David Spickett via flang-commits flang-commits at lists.llvm.org
Tue Jul 8 07:47:37 PDT 2025


================
@@ -214,16 +215,18 @@ template <typename CHAR> void FormatValidator<CHAR>::NextToken() {
   case '7':
   case '8':
   case '9': {
-    int64_t lastValue;
     const CHAR *lastCursor;
     integerValue_ = 0;
     bool overflow{false};
     do {
-      lastValue = integerValue_;
       lastCursor = cursor_;
-      integerValue_ = 10 * integerValue_ + c - '0';
-      if (lastValue > integerValue_) {
-        overflow = true;
+      if (LLVM_LIKELY(!overflow)) {
+        overflow = llvm::MulOverflow(
+            static_cast<int64_t>(10), integerValue_, integerValue_);
+      }
+      if (LLVM_LIKELY(!overflow)) {
+        overflow = llvm::AddOverflow(
+            integerValue_, static_cast<int64_t>(c - '0'), integerValue_);
----------------
DavidSpickett wrote:

`overflow = overflow || MulOverflow() || AddOverflow()` would do the latching properly but is more work to comprehend.

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


More information about the flang-commits mailing list