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

via flang-commits flang-commits at lists.llvm.org
Mon Dec 11 11:28:42 PST 2023


Author: Peter Klausler
Date: 2023-12-11T11:28:38-08:00
New Revision: 10f15e2ec419328aa35292b64721e1e1e9e37d70

URL: https://github.com/llvm/llvm-project/commit/10f15e2ec419328aa35292b64721e1e1e9e37d70
DIFF: https://github.com/llvm/llvm-project/commit/10f15e2ec419328aa35292b64721e1e1e9e37d70.diff

LOG: [flang][runtime] Crash more informatively in defined I/O error case (#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.

Added: 
    

Modified: 
    flang/include/flang/Runtime/iostat.h
    flang/runtime/descriptor-io.cpp
    flang/runtime/iostat.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Runtime/iostat.h b/flang/include/flang/Runtime/iostat.h
index 0456e24f4e381b..afce509cf1f564 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 563a69e999d5f4..3093a23a035137 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 cc5641693a078a..c993b778e9e1f8 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