diff --git a/flang/include/flang/Runtime/iostat.h b/flang/include/flang/Runtime/iostat.h --- a/flang/include/flang/Runtime/iostat.h +++ b/flang/include/flang/Runtime/iostat.h @@ -69,6 +69,7 @@ IostatUTF8Decoding, IostatUnitOverflow, IostatBadRealInput, + IostatBadScaleFactor, }; const char *IostatErrorString(int); diff --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp --- a/flang/runtime/edit-output.cpp +++ b/flang/runtime/edit-output.cpp @@ -196,9 +196,21 @@ int scale{isEN || isES ? 1 : edit.modes.scale}; // 'kP' value int zeroesAfterPoint{0}; if (scale < 0) { + if (scale <= -editDigits) { + io_.GetIoErrorHandler().SignalError(IostatBadScaleFactor, + "Scale factor (kP) %d cannot be less than -d (%d)", scale, + -editDigits); + return false; + } zeroesAfterPoint = -scale; significantDigits = std::max(0, significantDigits - zeroesAfterPoint); } else if (scale > 0) { + if (scale >= editDigits + 2) { + io_.GetIoErrorHandler().SignalError(IostatBadScaleFactor, + "Scale factor (kP) %d cannot be greater than d+2 (%d)", scale, + editDigits + 2); + return false; + } ++significantDigits; scale = std::min(scale, significantDigits + 1); } diff --git a/flang/runtime/iostat.cpp b/flang/runtime/iostat.cpp --- a/flang/runtime/iostat.cpp +++ b/flang/runtime/iostat.cpp @@ -81,6 +81,8 @@ return "UNIT number is out of range"; case IostatBadRealInput: return "Bad REAL input value"; + case IostatBadScaleFactor: + return "Bad REAL output scale factor (kP)"; default: return nullptr; }