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

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Jan 25 09:16:16 PST 2024


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

>From e43917bfd5d2f3c636a0064cee2b1fea371ea931 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Thu, 25 Jan 2024 09:00:09 -0800
Subject: [PATCH] [flang][runtime] Fix integer overflow check for FORMATs

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.
---
 flang/runtime/format-implementation.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/flang/runtime/format-implementation.h b/flang/runtime/format-implementation.h
index c54ac062c7beab9..9c342db2e19a246 100644
--- a/flang/runtime/format-implementation.h
+++ b/flang/runtime/format-implementation.h
@@ -85,8 +85,9 @@ 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() - 10 * result) {
       handler.SignalError(
           IostatErrorInFormat, "FORMAT integer field out of range");
       if (hadError) {



More information about the flang-commits mailing list