Skip to content

Commit

Permalink
[flang] EOF goes to ERR= in READ(..., REC=) (#122608)
Browse files Browse the repository at this point in the history
A direct access READ that tries to read past the end of the file must
recover the error via an ERR= label, not an END= label (which is not
allowed to be present).

Fixes #122150.
  • Loading branch information
klausler authored Jan 14, 2025
1 parent 9696355 commit bf95854
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions flang/runtime/io-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ bool IODEF(SetRec)(Cookie cookie, std::int64_t rec) {
handler.SignalError(
IostatBadOpOnChildUnit, "REC= specifier on child I/O");
} else {
handler.HasRec();
unit->SetDirectRec(rec, handler);
}
} else if (!io.get_if<ErroneousIoStatementState>()) {
Expand Down
4 changes: 3 additions & 1 deletion flang/runtime/io-error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ void IoErrorHandler::SignalError(int iostatOrErrno, const char *msg, ...) {
case IostatOk:
return;
case IostatEnd:
if (flags_ & (hasIoStat | hasEnd)) {
if ((flags_ & (hasIoStat | hasEnd)) ||
((flags_ & hasErr) && (flags_ & hasRec))) {
// EOF goes to ERR= when REC= is present
if (ioStat_ == IostatOk || ioStat_ < IostatEnd) {
ioStat_ = IostatEnd;
}
Expand Down
2 changes: 2 additions & 0 deletions flang/runtime/io-error.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class IoErrorHandler : public Terminator {
RT_API_ATTRS void HasEndLabel() { flags_ |= hasEnd; }
RT_API_ATTRS void HasEorLabel() { flags_ |= hasEor; }
RT_API_ATTRS void HasIoMsg() { flags_ |= hasIoMsg; }
RT_API_ATTRS void HasRec() { flags_ |= hasRec; }

RT_API_ATTRS bool InError() const {
return ioStat_ != IostatOk || pendingError_ != IostatOk;
Expand Down Expand Up @@ -70,6 +71,7 @@ class IoErrorHandler : public Terminator {
hasEnd = 4, // END=
hasEor = 8, // EOR=
hasIoMsg = 16, // IOMSG=
hasRec = 32, // REC=
};
std::uint8_t flags_{0};
int ioStat_{IostatOk};
Expand Down

0 comments on commit bf95854

Please sign in to comment.