diff --git a/flang/runtime/edit-output.h b/flang/runtime/edit-output.h --- a/flang/runtime/edit-output.h +++ b/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 &); 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 @@ -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( @@ -353,7 +355,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 &&