diff --git a/flang/runtime/buffer.h b/flang/runtime/buffer.h --- a/flang/runtime/buffer.h +++ b/flang/runtime/buffer.h @@ -128,6 +128,15 @@ } } + 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(*this); } diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp --- a/flang/runtime/unit.cpp +++ b/flang/runtime/unit.cpp @@ -898,9 +898,9 @@ } 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; }