[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 08:38:00 PDT 2024


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

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.)

>From 66aa7f9ede0b8322032484439a44d30a3ea9bbea 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-stmt.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

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