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

via flang-commits flang-commits at lists.llvm.org
Thu Jan 25 17:00:12 PST 2024


Author: Peter Klausler
Date: 2024-01-25T17:00:07-08:00
New Revision: 32334b91922b2c48665d04369b7858324820baa5

URL: https://github.com/llvm/llvm-project/commit/32334b91922b2c48665d04369b7858324820baa5
DIFF: https://github.com/llvm/llvm-project/commit/32334b91922b2c48665d04369b7858324820baa5.diff

LOG: [flang][runtime] Fix integer overflow check for FORMATs (#79471)

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.

Added: 
    

Modified: 
    flang/runtime/format-implementation.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/format-implementation.h b/flang/runtime/format-implementation.h
index c54ac062c7beab..9c342db2e19a24 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