[flang-commits] [flang] 9a163ff - [flang][runtime] Fix WRITE after OPEN(.., ACCESS="APPEND")

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sat Jun 4 09:25:37 PDT 2022


Author: Peter Klausler
Date: 2022-06-04T09:18:25-07:00
New Revision: 9a163ffe1a507c1f5a3d29b896224112d7e54f62

URL: https://github.com/llvm/llvm-project/commit/9a163ffe1a507c1f5a3d29b896224112d7e54f62
DIFF: https://github.com/llvm/llvm-project/commit/9a163ffe1a507c1f5a3d29b896224112d7e54f62.diff

LOG: [flang][runtime] Fix WRITE after OPEN(.., ACCESS="APPEND")

The initial size of the file was not being captured as the file position
on which the first output buffer should be framed.

Differential Revision: https://reviews.llvm.org/D127029

Added: 
    

Modified: 
    flang/runtime/file.cpp
    flang/runtime/unit.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp
index fa040b417c31..3b495c37d082 100644
--- a/flang/runtime/file.cpp
+++ b/flang/runtime/file.cpp
@@ -132,7 +132,7 @@ void OpenFile::Open(OpenStatus status, std::optional<Action> action,
   RUNTIME_CHECK(handler, action.has_value());
   pending_.reset();
   if (position == Position::Append && !RawSeekToEnd()) {
-    handler.SignalErrno();
+    handler.SignalError(IostatOpenBadAppend);
   }
   isTerminal_ = ::isatty(fd_) == 1;
   mayRead_ = *action != Action::Write;

diff  --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index c2f9d37bb122..73a96b8bf3dc 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -137,7 +137,7 @@ void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is invalid",
           unitNumber(), static_cast<std::intmax_t>(*openRecl));
     } else if (totalBytes && (*totalBytes % *openRecl != 0)) {
-      handler.SignalError(IostatOpenBadAppend,
+      handler.SignalError(IostatOpenBadRecl,
           "OPEN(UNIT=%d,ACCESS='DIRECT',RECL=%jd): record length is not an "
           "even divisor of the file size %jd",
           unitNumber(), static_cast<std::intmax_t>(*openRecl),
@@ -150,12 +150,17 @@ void ExternalFileUnit::OpenUnit(std::optional<OpenStatus> status,
   if (totalBytes && access == Access::Direct && openRecl.value_or(0) > 0) {
     endfileRecordNumber = 1 + (*totalBytes / *openRecl);
   }
-  if (position == Position::Append && access != Access::Stream) {
-    if (!endfileRecordNumber) {
-      // Fake it so that we can backspace relative from the end
-      endfileRecordNumber = std::numeric_limits<std::int64_t>::max() - 2;
+  if (position == Position::Append) {
+    if (totalBytes) {
+      frameOffsetInFile_ = *totalBytes;
+    }
+    if (access != Access::Stream) {
+      if (!endfileRecordNumber) {
+        // Fake it so that we can backspace relative from the end
+        endfileRecordNumber = std::numeric_limits<std::int64_t>::max() - 2;
+      }
+      currentRecordNumber = *endfileRecordNumber;
     }
-    currentRecordNumber = *endfileRecordNumber;
   }
 }
 


        


More information about the flang-commits mailing list