Skip to content
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

[flang] EOF goes to ERR= in READ(..., REC=) #122608

Merged
merged 1 commit into from
Jan 14, 2025
Merged

Conversation

klausler
Copy link
Contributor

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.

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 llvm#122150.
@klausler klausler requested a review from vdonaldson January 11, 2025 19:14
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jan 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 11, 2025

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/122608.diff

3 Files Affected:

  • (modified) flang/runtime/io-api.cpp (+1)
  • (modified) flang/runtime/io-error.cpp (+3-1)
  • (modified) flang/runtime/io-error.h (+2)
diff --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp
index 39ac8c9eb6defb..7023f61ba34de7 100644
--- a/flang/runtime/io-api.cpp
+++ b/flang/runtime/io-api.cpp
@@ -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>()) {
diff --git a/flang/runtime/io-error.cpp b/flang/runtime/io-error.cpp
index 7a90966f81047f..37909e8e6dad2c 100644
--- a/flang/runtime/io-error.cpp
+++ b/flang/runtime/io-error.cpp
@@ -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;
       }
diff --git a/flang/runtime/io-error.h b/flang/runtime/io-error.h
index 426573e2faf00c..39a343c8e0a516 100644
--- a/flang/runtime/io-error.h
+++ b/flang/runtime/io-error.h
@@ -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;
@@ -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};

@klausler klausler merged commit bf95854 into llvm:main Jan 14, 2025
11 checks passed
@klausler klausler deleted the bug122150 branch January 14, 2025 20:58
paulhuggett pushed a commit to paulhuggett/llvm-project that referenced this pull request Jan 16, 2025
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 llvm#122150.
DKLoehr pushed a commit to DKLoehr/llvm-project that referenced this pull request Jan 17, 2025
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 llvm#122150.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang][runtime][IO] OPEN statement core dumps at runtime with not-existent file
3 participants