Index: flang/runtime/io-stmt.cpp =================================================================== --- flang/runtime/io-stmt.cpp +++ flang/runtime/io-stmt.cpp @@ -315,10 +315,9 @@ } if constexpr (DIR == Direction::Input) { BeginReadingRecord(); // in case there were no I/O items - if (mutableModes().nonAdvancing) { + if (mutableModes().nonAdvancing && !InError()) { unit().leftTabLimit = unit().furthestPositionInRecord; - } - if (!mutableModes().nonAdvancing || GetIoStat() == IostatEor) { + } else { FinishReadingRecord(); } } else { // output Index: flang/runtime/unit.cpp =================================================================== --- flang/runtime/unit.cpp +++ flang/runtime/unit.cpp @@ -458,14 +458,14 @@ void ExternalFileUnit::FinishReadingRecord(IoErrorHandler &handler) { RUNTIME_CHECK(handler, direction_ == Direction::Input && beganReadingRecord_); beganReadingRecord_ = false; - if (handler.InError() && handler.GetIoStat() != IostatEor) { + if (handler.GetIoStat() == IostatEnd || + (IsRecordFile() && !recordLength.has_value())) { // Avoid bogus crashes in END/ERR circumstances; but // still increment the current record number so that // an attempted read of an endfile record, followed by // a BACKSPACE, will still be at EOF. ++currentRecordNumber; } else if (IsRecordFile()) { - RUNTIME_CHECK(handler, recordLength.has_value()); recordOffsetInFrame_ += *recordLength; if (access != Access::Direct) { RUNTIME_CHECK(handler, isUnformatted.has_value());