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

via flang-commits flang-commits at lists.llvm.org
Wed Sep 18 12:19:21 PDT 2024


Author: Peter Klausler
Date: 2024-09-18T12:19:18-07:00
New Revision: 5f11d38d019b8447a3f76c978a5beae4639015de

URL: https://github.com/llvm/llvm-project/commit/5f11d38d019b8447a3f76c978a5beae4639015de
DIFF: https://github.com/llvm/llvm-project/commit/5f11d38d019b8447a3f76c978a5beae4639015de.diff

LOG: [flang] Fix code that deletes unit from bad OPEN (#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.)

Added: 
    

Modified: 
    flang/runtime/io-api.cpp
    flang/runtime/io-stmt.cpp

Removed: 
    


################################################################################
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