[flang][runtime] Keep frame buffer in sync with file when truncating

When the I/O runtime is truncating an external file due to an
implied ENDFILE or explicit ENDFILE, ensure that the unit's frame
buffer for the file discards any data that have become obsolete.

This bug caused trouble with ACCESS='STREAM' I/O using POS= on
a WRITE, but it may have not been limited to that scenario.

Differential Revision: https://reviews.llvm.org/D129673
This commit is contained in:
Peter Klausler 2022-07-07 14:51:40 -07:00
parent faffcc3a46
commit 1a65d09dcf
2 changed files with 11 additions and 2 deletions

View File

@ -128,6 +128,15 @@ public:
}
}
void TruncateFrame(std::int64_t at, IoErrorHandler &handler) {
RUNTIME_CHECK(handler, !dirty_);
if (at <= fileOffset_) {
Reset(at);
} else if (at < fileOffset_ + length_) {
length_ = at - fileOffset_;
}
}
private:
STORE &Store() { return static_cast<STORE &>(*this); }

View File

@ -898,9 +898,9 @@ void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
}
frameOffsetInFile_ += recordOffsetInFrame_ + furthestPositionInRecord;
recordOffsetInFrame_ = 0;
// Flush (if dirty) and reset the frame (even if reading)
WriteFrame(frameOffsetInFile_, 0, handler);
FlushOutput(handler);
Truncate(frameOffsetInFile_, handler);
TruncateFrame(frameOffsetInFile_, handler);
BeginRecord();
impliedEndfile_ = false;
}