[flang-commits] [flang] 4c42e67 - [flang][runtime] Fix overflow detection for REAL input

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Jun 13 16:09:56 PDT 2022


Author: Peter Klausler
Date: 2022-06-13T16:05:11-07:00
New Revision: 4c42e67bf7f597a5b9b9aa51f2ea994a17c0aa54

URL: https://github.com/llvm/llvm-project/commit/4c42e67bf7f597a5b9b9aa51f2ea994a17c0aa54
DIFF: https://github.com/llvm/llvm-project/commit/4c42e67bf7f597a5b9b9aa51f2ea994a17c0aa54.diff

LOG: [flang][runtime] Fix overflow detection for REAL input

The test for an overflow during decimal->binary conversion was taking
place too late, causing the data not to be rescanned from the beginning.

Differential Revision: https://reviews.llvm.org/D127427

Added: 
    

Modified: 
    flang/runtime/edit-input.cpp
    flang/runtime/io-stmt.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/edit-input.cpp b/flang/runtime/edit-input.cpp
index 3c8630cebc59..cfcb98b68144 100644
--- a/flang/runtime/edit-input.cpp
+++ b/flang/runtime/edit-input.cpp
@@ -297,7 +297,9 @@ static int ScanRealInput(char *buffer, int bufferSize, IoStatementState &io,
       }
       for (exponent = 0; next; next = io.NextInField(remaining, edit)) {
         if (*next >= '0' && *next <= '9') {
-          exponent = 10 * exponent + *next - '0';
+          if (exponent < 10000) {
+            exponent = 10 * exponent + *next - '0';
+          }
         } else if (*next == ' ' || *next == '\t') {
           if (bzMode) {
             exponent = 10 * exponent;
@@ -392,7 +394,7 @@ static bool TryFastPathRealInput(
   const char *limit{str + maxConsume};
   decimal::ConversionToBinaryResult<PRECISION> converted{
       decimal::ConvertToBinary<PRECISION>(p, edit.modes.round, limit)};
-  if (converted.flags & decimal::Invalid) {
+  if (converted.flags & (decimal::Invalid | decimal::Overflow)) {
     return false;
   }
   if (edit.digits.value_or(0) != 0) {
@@ -428,9 +430,6 @@ static bool TryFastPathRealInput(
   io.HandleRelativePosition(p - str);
   // Set FP exception flags
   if (converted.flags != decimal::ConversionResultFlags::Exact) {
-    if (converted.flags & decimal::ConversionResultFlags::Overflow) {
-      return false; // let slow path deal with it
-    }
     RaiseFPExceptions(converted.flags);
   }
   return true;

diff  --git a/flang/runtime/io-stmt.h b/flang/runtime/io-stmt.h
index aa9f018dafbc..5e3209b7acab 100644
--- a/flang/runtime/io-stmt.h
+++ b/flang/runtime/io-stmt.h
@@ -136,7 +136,7 @@ class IoStatementState {
   std::optional<char32_t> PrepareInput(
       const DataEdit &edit, std::optional<int> &remaining) {
     remaining.reset();
-    if (edit.descriptor == DataEdit::ListDirected) {
+    if (edit.IsListDirected()) {
       std::size_t byteCount{0};
       GetNextNonBlank(byteCount);
     } else {


        


More information about the flang-commits mailing list