[flang-commits] [flang] [flang] Fix code that deletes unit from bad OPEN (PR #108994)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Sep 17 14:26:15 PDT 2024


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/108994

>From df03c6997002f461dfb6c5e33bebc68dfadffb1d Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 17 Sep 2024 08:34:14 -0700
Subject: [PATCH] [flang] Fix code that deletes unit from bad OPEN

When an OPEN statement fails, a unit that was created for the OPEN
needs to be removed from the unit map.  The code that tried to do
this was incorrect -- it needs to re-acquire the unit via LookUpForClose
as a CLOSE statement does.  (The failure to do this completely was
leaving a zombie unit active that could break a later OPEN on the
same unit number.)
---
 flang/runtime/io-api.cpp  | 2 +-
 flang/runtime/io-stmt.cpp | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp
index e3c6b9e5ca8959..39ac8c9eb6defb 100644
--- a/flang/runtime/io-api.cpp
+++ b/flang/runtime/io-api.cpp
@@ -948,7 +948,7 @@ bool IODEF(SetRecl)(Cookie cookie, std::size_t n) {
     io.GetIoErrorHandler().Crash(
         "SetRecl() called after GetNewUnit() for an OPEN statement");
   }
-  if (n <= 0) {
+  if (static_cast<std::int64_t>(n) <= 0) {
     io.GetIoErrorHandler().SignalError("RECL= must be greater than zero");
     return false;
   } else if (open->wasExtant() &&
diff --git a/flang/runtime/io-stmt.cpp b/flang/runtime/io-stmt.cpp
index 265bd0dc9d9499..cd7a196335d31e 100644
--- a/flang/runtime/io-stmt.cpp
+++ b/flang/runtime/io-stmt.cpp
@@ -329,8 +329,11 @@ void OpenStatementState::CompleteOperation() {
   }
   if (!wasExtant_ && InError()) {
     // Release the new unit on failure
-    unit().CloseUnit(CloseStatus::Delete, *this);
-    unit().DestroyClosed();
+    if (ExternalFileUnit *
+        toClose{unit().LookUpForClose(unit().unitNumber())}) {
+      toClose->Close(CloseStatus::Delete, *this);
+      toClose->DestroyClosed();
+    }
   }
   IoStatementBase::CompleteOperation();
 }



More information about the flang-commits mailing list