[flang-commits] [flang] db52dda - [flang][runtime] Detect overflow of fixed-sized exponent output field

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


Author: Peter Klausler
Date: 2022-06-13T16:10:32-07:00
New Revision: db52dda8ab6f99c4b4fd0515c498c435168688e7

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

LOG: [flang][runtime] Detect overflow of fixed-sized exponent output field

When Ew.dEe or Gw.dEe output has an exponent requiring more than 'e'
digits, the whole output field must overflow to asterisks.  The runtime
was detecting short fields and padding them with zeroes, but not
overflow.

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

Added: 
    

Modified: 
    flang/runtime/edit-output.cpp
    flang/runtime/edit-output.h

Removed: 
    


################################################################################
diff  --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp
index b26753cdc7b8..9b25ef2fba16 100644
--- a/flang/runtime/edit-output.cpp
+++ b/flang/runtime/edit-output.cpp
@@ -182,8 +182,10 @@ const char *RealOutputEditingBase::FormatExponent(
     *--exponent = '0' + e - 10 * quotient;
     e = quotient;
   }
+  bool overflow{false};
   if (edit.expoDigits) {
     if (int ed{*edit.expoDigits}) { // Ew.dEe with e > 0
+      overflow = exponent + ed < eEnd;
       while (exponent > exponent_ + 2 /*E+*/ && exponent + ed > eEnd) {
         *--exponent = '0';
       }
@@ -200,7 +202,7 @@ const char *RealOutputEditingBase::FormatExponent(
     *--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G'
   }
   length = eEnd - exponent;
-  return exponent;
+  return overflow ? nullptr : exponent;
 }
 
 bool RealOutputEditingBase::EmitPrefix(
@@ -353,7 +355,7 @@ bool RealOutputEditing<binaryPrecision>::EditEorDOutput(const DataEdit &edit) {
         1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes +
         expoLength};
     int width{editWidth > 0 ? editWidth : totalLength};
-    if (totalLength > width) {
+    if (totalLength > width || !exponent) {
       return io_.EmitRepeated('*', width);
     }
     if (totalLength < width && digitsBeforePoint == 0 &&

diff  --git a/flang/runtime/edit-output.h b/flang/runtime/edit-output.h
index bd1377e3a18c..765e41f89827 100644
--- a/flang/runtime/edit-output.h
+++ b/flang/runtime/edit-output.h
@@ -52,6 +52,7 @@ class RealOutputEditingBase {
     return *p < '0' || *p > '9';
   }
 
+  // Returns null when the exponent overflows a fixed-size output field.
   const char *FormatExponent(int, const DataEdit &edit, int &length);
   bool EmitPrefix(const DataEdit &, std::size_t length, std::size_t width);
   bool EmitSuffix(const DataEdit &);


        


More information about the flang-commits mailing list