[flang-commits] [PATCH] D129673: [flang][runtime] Keep frame buffer in sync with file when truncating

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Jul 13 11:38:16 PDT 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

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.


https://reviews.llvm.org/D129673

Files:
  flang/runtime/buffer.h
  flang/runtime/unit.cpp


Index: flang/runtime/unit.cpp
===================================================================
--- flang/runtime/unit.cpp
+++ 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;
 }
Index: flang/runtime/buffer.h
===================================================================
--- flang/runtime/buffer.h
+++ 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<STORE &>(*this); }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129673.444353.patch
Type: text/x-patch
Size: 1055 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220713/db4d1f51/attachment.bin>


More information about the flang-commits mailing list