[flang-commits] [PATCH] D145754: [flang][runtime] EOF is recoverable only with END= or IOSTAT=

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Mar 9 16:40:54 PST 2023


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

The runtime's I/O error handler was not crashing the program on an  
end-of-file condition that arises for a data transfer statement with
at least one of ERR= or IOMSG= and none of END= or IOSTAT= control
items.  This turns out to be incorrect (per subclause 12.11);
an EOF is recoverable only for END= and/or IOSTAT=, and an
non-advancing end-of-record is recoverable only for EOR= and/or IOSTAT=.


https://reviews.llvm.org/D145754

Files:
  flang/runtime/io-error.cpp


Index: flang/runtime/io-error.cpp
===================================================================
--- flang/runtime/io-error.cpp
+++ flang/runtime/io-error.cpp
@@ -18,16 +18,29 @@
 namespace Fortran::runtime::io {
 
 void IoErrorHandler::SignalError(int iostatOrErrno, const char *msg, ...) {
-  if (iostatOrErrno == IostatEnd && (flags_ & hasEnd)) {
-    if (ioStat_ == IostatOk || ioStat_ < IostatEnd) {
-      ioStat_ = IostatEnd;
+  // Note that IOMSG= alone without IOSTAT=/END=/EOR=/ERR= does not suffice
+  // for error recovery (see F'2018 subclause 12.11).
+  switch (iostatOrErrno) {
+  case IostatOk:
+    return;
+  case IostatEnd:
+    if (flags_ & (hasIoStat | hasEnd)) {
+      if (ioStat_ == IostatOk || ioStat_ < IostatEnd) {
+        ioStat_ = IostatEnd;
+      }
+      return;
     }
-  } else if (iostatOrErrno == IostatEor && (flags_ & hasEor)) {
-    if (ioStat_ == IostatOk || ioStat_ < IostatEor) {
-      ioStat_ = IostatEor; // least priority
+    break;
+  case IostatEor:
+    if (flags_ & (hasIoStat | hasEor)) {
+      if (ioStat_ == IostatOk || ioStat_ < IostatEor) {
+        ioStat_ = IostatEor; // least priority
+      }
+      return;
     }
-  } else if (iostatOrErrno != IostatOk) {
-    if (flags_ & (hasIoStat | hasIoMsg | hasErr)) {
+    break;
+  default:
+    if (flags_ & (hasIoStat | hasErr)) {
       if (ioStat_ <= 0) {
         ioStat_ = iostatOrErrno; // priority over END=/EOR=
         if (msg && (flags_ & hasIoMsg)) {
@@ -39,17 +52,21 @@
           va_end(ap);
         }
       }
-    } else if (msg) {
-      va_list ap;
-      va_start(ap, msg);
-      CrashArgs(msg, ap);
-      va_end(ap);
-    } else if (const char *errstr{IostatErrorString(iostatOrErrno)}) {
-      Crash(errstr);
-    } else {
-      Crash("I/O error (errno=%d): %s", iostatOrErrno,
-          std::strerror(iostatOrErrno));
+      return;
     }
+    break;
+  }
+  // I/O error not caught!
+  if (msg) {
+    va_list ap;
+    va_start(ap, msg);
+    CrashArgs(msg, ap);
+    va_end(ap);
+  } else if (const char *errstr{IostatErrorString(iostatOrErrno)}) {
+    Crash(errstr);
+  } else {
+    Crash("I/O error (errno=%d): %s", iostatOrErrno,
+        std::strerror(iostatOrErrno));
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145754.503980.patch
Type: text/x-patch
Size: 2229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230310/6608f574/attachment.bin>


More information about the flang-commits mailing list