[flang-commits] [flang] [flang][runtime] Fix integer overflow check for FORMATs (PR #79471)

via flang-commits flang-commits at lists.llvm.org
Thu Jan 25 09:03:38 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

The code that parses repeat counts, field widths, &c. from FORMAT strings has an incorrect overflow check, so the maximum integer value is not accepted.  Fix.

Fixes https://github.com/llvm/llvm-project/issues/79255.

---
Full diff: https://github.com/llvm/llvm-project/pull/79471.diff


1 Files Affected:

- (modified) flang/runtime/format-implementation.h (+2-2) 


``````````diff
diff --git a/flang/runtime/format-implementation.h b/flang/runtime/format-implementation.h
index c54ac062c7beab..b0a4f6b9eb2f79 100644
--- a/flang/runtime/format-implementation.h
+++ b/flang/runtime/format-implementation.h
@@ -85,8 +85,8 @@ int FormatControl<CONTEXT>::GetIntField(
     ch = PeekNext();
   }
   while (ch >= '0' && ch <= '9') {
-    if (result >
-        std::numeric_limits<int>::max() / 10 - (static_cast<int>(ch) - '0')) {
+    constexpr int tenth{std::numeric_limits<int>::max() / 10};
+    if (result > tenth || ch - '0' > std::numeric_limits<int>::max() - result) {
       handler.SignalError(
           IostatErrorInFormat, "FORMAT integer field out of range");
       if (hadError) {

``````````

</details>


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


More information about the flang-commits mailing list