Skip to content

Commit 5f11d38

Browse files
authored
[flang] Fix code that deletes unit from bad OPEN (llvm#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.)
1 parent b0bdc7f commit 5f11d38

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

flang/runtime/io-api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ bool IODEF(SetRecl)(Cookie cookie, std::size_t n) {
948948
io.GetIoErrorHandler().Crash(
949949
"SetRecl() called after GetNewUnit() for an OPEN statement");
950950
}
951-
if (n <= 0) {
951+
if (static_cast<std::int64_t>(n) <= 0) {
952952
io.GetIoErrorHandler().SignalError("RECL= must be greater than zero");
953953
return false;
954954
} else if (open->wasExtant() &&

flang/runtime/io-stmt.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,11 @@ void OpenStatementState::CompleteOperation() {
329329
}
330330
if (!wasExtant_ && InError()) {
331331
// Release the new unit on failure
332-
unit().CloseUnit(CloseStatus::Delete, *this);
333-
unit().DestroyClosed();
332+
if (ExternalFileUnit *
333+
toClose{unit().LookUpForClose(unit().unitNumber())}) {
334+
toClose->Close(CloseStatus::Delete, *this);
335+
toClose->DestroyClosed();
336+
}
334337
}
335338
IoStatementBase::CompleteOperation();
336339
}

0 commit comments

Comments
 (0)