[flang-commits] [flang] [flang][runtime] Crash more informatively in defined I/O error case (PR #74134)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Fri Dec 1 11:48:42 PST 2023
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/74134
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.
>From 63e4211a54e896b611107e8db1cd659b6200830f Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Fri, 1 Dec 2023 11:44:26 -0800
Subject: [PATCH] [flang][runtime] Crash more informatively in defined I/O
error case
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.
---
flang/include/flang/Runtime/iostat.h | 1 +
flang/runtime/descriptor-io.cpp | 5 ++++-
flang/runtime/iostat.cpp | 2 ++
3 files changed, 7 insertions(+), 1 deletion(-)
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;
}
More information about the flang-commits
mailing list