Skip to content

[flang][runtime] Fix runtime crash after bad recoverable OPEN #111454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions flang/runtime/io-stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,15 @@ int ExternalIoStatementBase::EndIoStatement() {
CompleteOperation();
auto result{IoStatementBase::EndIoStatement()};
#if !defined(RT_USE_PSEUDO_FILE_UNIT)
auto unitNumber{unit_.unitNumber()};
unit_.EndIoStatement(); // annihilates *this in unit_.u_
if (destroy_) {
if (ExternalFileUnit *
toClose{ExternalFileUnit::LookUpForClose(unitNumber)}) {
toClose->Close(CloseStatus::Delete, *this);
toClose->DestroyClosed();
}
}
#else
// Fetch the unit pointer before *this disappears.
ExternalFileUnit *unitPtr{&unit_};
Expand Down Expand Up @@ -329,11 +337,7 @@ void OpenStatementState::CompleteOperation() {
}
if (!wasExtant_ && InError()) {
// Release the new unit on failure
if (ExternalFileUnit *
toClose{unit().LookUpForClose(unit().unitNumber())}) {
toClose->Close(CloseStatus::Delete, *this);
toClose->DestroyClosed();
}
set_destroy();
}
IoStatementBase::CompleteOperation();
}
Expand Down
2 changes: 2 additions & 0 deletions flang/runtime/io-stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ class ExternalIoStatementBase : public IoStatementBase {
RT_API_ATTRS MutableModes &mutableModes();
RT_API_ATTRS ConnectionState &GetConnectionState();
RT_API_ATTRS int asynchronousID() const { return asynchronousID_; }
RT_API_ATTRS void set_destroy(bool yes = true) { destroy_ = yes; }
RT_API_ATTRS int EndIoStatement();
RT_API_ATTRS ExternalFileUnit *GetExternalFileUnit() const { return &unit_; }
RT_API_ATTRS void SetAsynchronous();
Expand All @@ -463,6 +464,7 @@ class ExternalIoStatementBase : public IoStatementBase {
private:
ExternalFileUnit &unit_;
int asynchronousID_{-1};
bool destroy_{false};
};

template <Direction DIR>
Expand Down
Loading