[flang-commits] [flang] [flang][runtime] Crash more informatively in defined I/O error case (PR #74134)

via flang-commits flang-commits at lists.llvm.org
Fri Dec 1 11:49:12 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

Defined unformatted I/O is not allowed except from/to an external unit. This restriction prohibits an INQUIRE(IOLENGTH=n) statement from using derived types with defined unformatted output in its I/O list.  The runtime currently detects this case and crashes with an internal error; this patch defines a new I/O error enum and causes the program to crash with a more useful message.

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


3 Files Affected:

- (modified) flang/include/flang/Runtime/iostat.h (+1) 
- (modified) flang/runtime/descriptor-io.cpp (+4-1) 
- (modified) flang/runtime/iostat.cpp (+2) 


``````````diff
diff --git a/flang/include/flang/Runtime/iostat.h b/flang/include/flang/Runtime/iostat.h
index 0456e24f4e381ba..afce509cf1f5640 100644
--- a/flang/include/flang/Runtime/iostat.h
+++ b/flang/include/flang/Runtime/iostat.h
@@ -85,6 +85,7 @@ enum Iostat {
   IostatBadOpOnChildUnit,
   IostatBadNewUnit,
   IostatBadListDirectedInputSeparator,
+  IostatNonExternalDefinedUnformattedIo,
 };
 
 const char *IostatErrorString(int);
diff --git a/flang/runtime/descriptor-io.cpp b/flang/runtime/descriptor-io.cpp
index 563a69e999d5f45..3093a23a035137b 100644
--- a/flang/runtime/descriptor-io.cpp
+++ b/flang/runtime/descriptor-io.cpp
@@ -119,7 +119,10 @@ bool DefinedUnformattedIo(IoStatementState &io, const Descriptor &descriptor,
   // Unformatted I/O must have an external unit (or child thereof).
   IoErrorHandler &handler{io.GetIoErrorHandler()};
   ExternalFileUnit *external{io.GetExternalFileUnit()};
-  RUNTIME_CHECK(handler, external != nullptr);
+  if (!external) { // INQUIRE(IOLENGTH=)
+    handler.SignalError(IostatNonExternalDefinedUnformattedIo);
+    return false;
+  }
   ChildIo &child{external->PushChildIo(io)};
   int unit{external->unitNumber()};
   int ioStat{IostatOk};
diff --git a/flang/runtime/iostat.cpp b/flang/runtime/iostat.cpp
index cc5641693a078a6..c993b778e9e1f8c 100644
--- a/flang/runtime/iostat.cpp
+++ b/flang/runtime/iostat.cpp
@@ -115,6 +115,8 @@ const char *IostatErrorString(int iostat) {
     return "NEWUNIT= without FILE= or STATUS='SCRATCH'";
   case IostatBadListDirectedInputSeparator:
     return "List-directed input value has trailing unused characters";
+  case IostatNonExternalDefinedUnformattedIo:
+    return "Defined unformatted I/O without an external unit";
   default:
     return nullptr;
   }

``````````

</details>


https://github.com/llvm/llvm-project/pull/74134


More information about the flang-commits mailing list