[flang-commits] [flang] 763e036 - [flang] Detect output field width overflow for Inf/NaN

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed May 31 09:09:05 PDT 2023


Author: Peter Klausler
Date: 2023-05-31T09:08:58-07:00
New Revision: 763e036cc9122f5a0acc650f2899f2210029164c

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

LOG: [flang] Detect output field width overflow for Inf/NaN

The output editing code paths for F and E/D output that handle
IEEE-754 infinities and NaNs fail to check for overflow of the
output field, which should cause the field to be filled with
asterisks instead.  Catch these cases.

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

Added: 
    

Modified: 
    flang/runtime/edit-output.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp
index 0b31df766087c..1625d8948b32f 100644
--- a/flang/runtime/edit-output.cpp
+++ b/flang/runtime/edit-output.cpp
@@ -320,8 +320,12 @@ bool RealOutputEditing<KIND>::EditEorDOutput(const DataEdit &edit) {
     decimal::ConversionToDecimalResult converted{
         Convert(significantDigits, edit.modes.round, flags)};
     if (IsInfOrNaN(converted)) {
-      return EmitPrefix(edit, converted.length, editWidth) &&
-          EmitAscii(io_, converted.str, converted.length) && EmitSuffix(edit);
+      return editWidth > 0 &&
+              converted.length > static_cast<std::size_t>(editWidth)
+          ? EmitRepeated(io_, '*', editWidth)
+          : EmitPrefix(edit, converted.length, editWidth) &&
+              EmitAscii(io_, converted.str, converted.length) &&
+              EmitSuffix(edit);
     }
     if (!IsZero()) {
       converted.decimalExponent -= scale;
@@ -415,8 +419,12 @@ bool RealOutputEditing<KIND>::EditFOutput(const DataEdit &edit) {
     decimal::ConversionToDecimalResult converted{
         Convert(extraDigits + fracDigits, rounding, flags)};
     if (IsInfOrNaN(converted)) {
-      return EmitPrefix(edit, converted.length, editWidth) &&
-          EmitAscii(io_, converted.str, converted.length) && EmitSuffix(edit);
+      return editWidth > 0 &&
+              converted.length > static_cast<std::size_t>(editWidth)
+          ? EmitRepeated(io_, '*', editWidth)
+          : EmitPrefix(edit, converted.length, editWidth) &&
+              EmitAscii(io_, converted.str, converted.length) &&
+              EmitSuffix(edit);
     }
     int expo{converted.decimalExponent + edit.modes.scale /*kP*/};
     int signLength{*converted.str == '-' || *converted.str == '+' ? 1 : 0};


        


More information about the flang-commits mailing list