diff --git a/flang/runtime/connection.h b/flang/runtime/connection.h --- a/flang/runtime/connection.h +++ b/flang/runtime/connection.h @@ -54,6 +54,7 @@ void BeginRecord() { positionInRecord = 0; furthestPositionInRecord = 0; + unterminatedRecord = false; } std::optional EffectiveRecordLength() const { @@ -85,6 +86,10 @@ // so that backspacing to the beginning of the repeated item doesn't require // repositioning the external storage medium when that's impossible. bool pinnedFrame{false}; + + // Set when the last record of a file is not properly terminated + // so that a non-advancing READ will not signal EOR. + bool unterminatedRecord{false}; }; // Utility class for capturing and restoring a position in an input stream. 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 @@ -327,7 +327,6 @@ } unit().leftTabLimit = unit().furthestPositionInRecord; } else { - unit().leftTabLimit.reset(); unit().AdvanceRecord(*this); } unit().FlushIfTerminal(*this); @@ -673,7 +672,15 @@ if (connection.positionInRecord >= *length) { IoErrorHandler &handler{GetIoErrorHandler()}; if (mutableModes().nonAdvancing) { - handler.SignalEor(); + if (connection.access == Access::Stream && + connection.unterminatedRecord) { + // Reading final unterminated record left by a + // non-advancing WRITE on a stream file prior to + // positioning or ENDFILE. + handler.SignalEnd(); + } else { + handler.SignalEor(); + } } else if (!connection.modes.pad) { handler.SignalError(IostatRecordReadOverrun); } diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp --- a/flang/runtime/unit.cpp +++ b/flang/runtime/unit.cpp @@ -349,7 +349,7 @@ return true; } else { handler.SignalEnd(); - if (access == Access::Sequential) { + if (IsRecordFile() && access != Access::Direct) { endfileRecordNumber = currentRecordNumber; } return false; @@ -385,7 +385,7 @@ return Frame() + at; } handler.SignalEnd(); - if (access == Access::Sequential) { + if (IsRecordFile() && access != Access::Direct) { endfileRecordNumber = currentRecordNumber; } } @@ -544,6 +544,7 @@ #endif ok = ok && Emit(lineEnding, lineEndingBytes, 1, handler); } + leftTabLimit.reset(); if (IsAfterEndfile()) { return false; } @@ -620,7 +621,7 @@ // ENDFILE after ENDFILE } else { DoEndfile(handler); - if (access == Access::Sequential) { + if (IsRecordFile() && access != Access::Direct) { // Explicit ENDFILE leaves position *after* the endfile record RUNTIME_CHECK(handler, endfileRecordNumber.has_value()); currentRecordNumber = *endfileRecordNumber + 1; @@ -716,6 +717,7 @@ if (length > 0) { // final record w/o \n recordLength = length; + unterminatedRecord = true; } else { handler.SignalEnd(); } @@ -839,11 +841,17 @@ } void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) { - if (IsRecordFile()) { + if (IsRecordFile() && access != Access::Direct) { + if (furthestPositionInRecord > 0) { + // Last write was non-advancing, so AdvanceRecord() was not called. + leftTabLimit.reset(); + ++currentRecordNumber; + } endfileRecordNumber = currentRecordNumber; } FlushOutput(handler); - Truncate(frameOffsetInFile_ + recordOffsetInFrame_, handler); + Truncate(frameOffsetInFile_ + recordOffsetInFrame_ + furthestPositionInRecord, + handler); BeginRecord(); impliedEndfile_ = false; }