[flang-commits] [PATCH] D127428: [flang][runtime] Detect overflow of fixed-sized exponent output field

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Jun 9 12:13:38 PDT 2022


klausler created this revision.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

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.


https://reviews.llvm.org/D127428

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


Index: flang/runtime/edit-output.h
===================================================================
--- flang/runtime/edit-output.h
+++ flang/runtime/edit-output.h
@@ -52,6 +52,7 @@
     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 &);
Index: flang/runtime/edit-output.cpp
===================================================================
--- flang/runtime/edit-output.cpp
+++ flang/runtime/edit-output.cpp
@@ -182,8 +182,10 @@
     *--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 @@
     *--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G'
   }
   length = eEnd - exponent;
-  return exponent;
+  return overflow ? nullptr : exponent;
 }
 
 bool RealOutputEditingBase::EmitPrefix(
@@ -352,7 +354,7 @@
         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 &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127428.435637.patch
Type: text/x-patch
Size: 1595 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220609/82b9eb18/attachment.bin>


More information about the flang-commits mailing list