diff --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp --- a/flang/runtime/io-stmt.cpp +++ b/flang/runtime/io-stmt.cpp @@ -319,10 +319,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 diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp --- a/flang/runtime/unit.cpp +++ b/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());