Index: flang/runtime/internal-unit.cpp =================================================================== --- flang/runtime/internal-unit.cpp +++ flang/runtime/internal-unit.cpp @@ -41,7 +41,9 @@ 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(); } }