[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
This commit is contained in:
Peter Klausler 2023-05-25 16:23:22 -07:00
parent a88f496f8f
commit 763e036cc9
No known key found for this signature in database

View File

@ -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};