Skip to content

[flang][runtime] Crash more informatively in defined I/O error case #74134

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
Dec 11, 2023

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Dec 1, 2023

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.

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.
@klausler klausler requested a review from vdonaldson December 1, 2023 19:48
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Dec 1, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2023

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

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 --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;
   }

@klausler klausler merged commit 10f15e2 into llvm:main Dec 11, 2023
@klausler klausler deleted the dtio16 branch December 11, 2023 19:28
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.

3 participants