Skip to content

Commit

Permalink
[flang][runtime] Don't write implied ENDFILE for REC=/POS= (#79637)
Browse files Browse the repository at this point in the history
An implied ENDFILE record, which truncates an external file, should be
written to a sequential unit whenever the file is repositioned for a
BACKSPACE or REWIND statement if a WRITE statement has executed since
the last OPEN/BACKSPACE/REWIND.

But the REC= and POS= positioning specifiers don't apply to sequential
units (they're for direct and stream units, resp.), so don't truncate
the file when they're used.
  • Loading branch information
klausler committed Feb 20, 2024
1 parent 1219214 commit 96b1704
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion flang/runtime/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
handler.SignalError(IostatRewindNonSequential,
"REWIND(UNIT=%d) on non-sequential file", unitNumber());
} else {
DoImpliedEndfile(handler);
SetPosition(0, handler);
currentRecordNumber = 1;
leftTabLimit.reset();
Expand All @@ -687,7 +688,6 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
}

void ExternalFileUnit::SetPosition(std::int64_t pos, IoErrorHandler &handler) {
DoImpliedEndfile(handler);
frameOffsetInFile_ = pos;
recordOffsetInFrame_ = 0;
if (access == Access::Direct) {
Expand All @@ -707,6 +707,12 @@ bool ExternalFileUnit::SetStreamPos(
"POS=%zd is invalid", static_cast<std::intmax_t>(oneBasedPos));
return false;
}
// A backwards POS= implies truncation after writing, at least in
// Intel and NAG.
if (static_cast<std::size_t>(oneBasedPos - 1) <
frameOffsetInFile_ + recordOffsetInFrame_) {
DoImpliedEndfile(handler);
}
SetPosition(oneBasedPos - 1, handler);
// We no longer know which record we're in. Set currentRecordNumber to
// a large value from whence we can both advance and backspace.
Expand Down

0 comments on commit 96b1704

Please sign in to comment.