diff --git a/flang/runtime/internal-unit.cpp b/flang/runtime/internal-unit.cpp index 39a8e4b2c9c4e..9f1d6c572c327 100644 --- a/flang/runtime/internal-unit.cpp +++ b/flang/runtime/internal-unit.cpp @@ -41,7 +41,9 @@ template void InternalDescriptorUnit::EndIoStatement() { if constexpr (DIR == Direction::Output) { // Clear the remainder of the current record if anything was written // to it, or if it is the only record. - if (endfileRecordNumber.value_or(-1) == 2 || furthestPositionInRecord > 0) { + auto end{endfileRecordNumber.value_or(0)}; + if (currentRecordNumber < end && + (end == 2 || furthestPositionInRecord > 0)) { BlankFillOutputRecord(); } }