[flang-commits] [flang] [llvm] [Flang] [Runtime ]Fix write endfile abort (PR #191633)

via flang-commits flang-commits at lists.llvm.org
Tue Apr 14 00:12:45 PDT 2026


https://github.com/blazie2004 updated https://github.com/llvm/llvm-project/pull/191633

>From 17c54cd23fab01a302955765edba64a9611aa417 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Sat, 11 Apr 2026 08:51:39 -0500
Subject: [PATCH 1/3] Fix WRITE after ENDFILE abort

---
 flang-rt/include/flang-rt/runtime/io-error.h |  4 +++
 flang-rt/lib/runtime/unit.cpp                |  2 +-
 flang/test/Runtime/write-after-endfile.f90   | 29 ++++++++++++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Runtime/write-after-endfile.f90

diff --git a/flang-rt/include/flang-rt/runtime/io-error.h b/flang-rt/include/flang-rt/runtime/io-error.h
index d2180a83f8c3c..4691a0ff5b8cd 100644
--- a/flang-rt/include/flang-rt/runtime/io-error.h
+++ b/flang-rt/include/flang-rt/runtime/io-error.h
@@ -34,6 +34,10 @@ class IoErrorHandler : public Terminator {
   RT_API_ATTRS void HasEorLabel() { flags_ |= hasEor; }
   RT_API_ATTRS void HasIoMsg() { flags_ |= hasIoMsg; }
   RT_API_ATTRS void HasRec() { flags_ |= hasRec; }
+  RT_API_ATTRS void SignalPendingIoStat(int iostat) {
+  if (ioStat_ == IostatOk || ioStat_ == IostatEnd || ioStat_ == IostatEor)
+    ioStat_ = iostat;
+}
 
   RT_API_ATTRS bool InError() const {
     return ioStat_ != IostatOk || pendingError_ != IostatOk;
diff --git a/flang-rt/lib/runtime/unit.cpp b/flang-rt/lib/runtime/unit.cpp
index c577ae7673127..32a48cd4fe3ef 100644
--- a/flang-rt/lib/runtime/unit.cpp
+++ b/flang-rt/lib/runtime/unit.cpp
@@ -84,7 +84,7 @@ bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
     beganReadingRecord_ = false;
   }
   if (IsAfterEndfile()) {
-    handler.SignalError(IostatWriteAfterEndfile);
+     handler.SignalPendingIoStat(IostatWriteAfterEndfile);
     return false;
   }
   CheckDirectAccess(handler);
diff --git a/flang/test/Runtime/write-after-endfile.f90 b/flang/test/Runtime/write-after-endfile.f90
new file mode 100644
index 0000000000000..c519bb4b649a0
--- /dev/null
+++ b/flang/test/Runtime/write-after-endfile.f90
@@ -0,0 +1,29 @@
+! RUN: %flang %s -o %t && %t | FileCheck %s
+! CHECK: k= {{ *}}2
+
+
+program test_write_after_endfile
+  implicit none
+  integer :: n, k
+
+  open(10, status='scratch')
+
+  write(10,*) 123
+  endfile(10)
+
+  ! This should NOT crash; should go to ERR label
+  write(10, err=100, iostat=n) 456
+  k = 1
+  goto 200
+
+100 continue
+  k = 2
+
+200 continue
+  print *, "k=", k
+end program
+
+
+
+
+

>From df4cde860af5aec51fce3a607f6828ce67190a83 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Sat, 11 Apr 2026 11:28:09 -0500
Subject: [PATCH 2/3] fixed formatting

---
 flang-rt/include/flang-rt/runtime/io-error.h         |  6 +++---
 flang-rt/lib/runtime/unit.cpp                        |  2 +-
 .../test/Runtime/write-after-endfile.f90             | 12 +++---------
 3 files changed, 7 insertions(+), 13 deletions(-)
 rename {flang => flang-rt}/test/Runtime/write-after-endfile.f90 (76%)

diff --git a/flang-rt/include/flang-rt/runtime/io-error.h b/flang-rt/include/flang-rt/runtime/io-error.h
index 4691a0ff5b8cd..088fc76a7909a 100644
--- a/flang-rt/include/flang-rt/runtime/io-error.h
+++ b/flang-rt/include/flang-rt/runtime/io-error.h
@@ -35,9 +35,9 @@ class IoErrorHandler : public Terminator {
   RT_API_ATTRS void HasIoMsg() { flags_ |= hasIoMsg; }
   RT_API_ATTRS void HasRec() { flags_ |= hasRec; }
   RT_API_ATTRS void SignalPendingIoStat(int iostat) {
-  if (ioStat_ == IostatOk || ioStat_ == IostatEnd || ioStat_ == IostatEor)
-    ioStat_ = iostat;
-}
+    if (ioStat_ == IostatOk || ioStat_ == IostatEnd || ioStat_ == IostatEor)
+      ioStat_ = iostat;
+  }
 
   RT_API_ATTRS bool InError() const {
     return ioStat_ != IostatOk || pendingError_ != IostatOk;
diff --git a/flang-rt/lib/runtime/unit.cpp b/flang-rt/lib/runtime/unit.cpp
index 32a48cd4fe3ef..5dfc1702234a3 100644
--- a/flang-rt/lib/runtime/unit.cpp
+++ b/flang-rt/lib/runtime/unit.cpp
@@ -84,7 +84,7 @@ bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
     beganReadingRecord_ = false;
   }
   if (IsAfterEndfile()) {
-     handler.SignalPendingIoStat(IostatWriteAfterEndfile);
+    handler.SignalPendingIoStat(IostatWriteAfterEndfile);
     return false;
   }
   CheckDirectAccess(handler);
diff --git a/flang/test/Runtime/write-after-endfile.f90 b/flang-rt/test/Runtime/write-after-endfile.f90
similarity index 76%
rename from flang/test/Runtime/write-after-endfile.f90
rename to flang-rt/test/Runtime/write-after-endfile.f90
index c519bb4b649a0..cf47a102c883e 100644
--- a/flang/test/Runtime/write-after-endfile.f90
+++ b/flang-rt/test/Runtime/write-after-endfile.f90
@@ -1,6 +1,5 @@
-! RUN: %flang %s -o %t && %t | FileCheck %s
-! CHECK: k= {{ *}}2
-
+! RUN: %flang %s -o %t 2>&1 | FileCheck %s --allow-empty
+! CHECK-NOT: error
 
 program test_write_after_endfile
   implicit none
@@ -21,9 +20,4 @@ program test_write_after_endfile
 
 200 continue
   print *, "k=", k
-end program
-
-
-
-
-
+end program
\ No newline at end of file

>From 622f9820ad2a622ddb967fbb0d76acbe9048d9d7 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Tue, 14 Apr 2026 02:12:00 -0500
Subject: [PATCH 3/3] updated testcase

---
 flang-rt/test/Runtime/write-after-endfile.f90 | 42 +++++++++----------
 1 file changed, 20 insertions(+), 22 deletions(-)

diff --git a/flang-rt/test/Runtime/write-after-endfile.f90 b/flang-rt/test/Runtime/write-after-endfile.f90
index cf47a102c883e..553bc735ead2d 100644
--- a/flang-rt/test/Runtime/write-after-endfile.f90
+++ b/flang-rt/test/Runtime/write-after-endfile.f90
@@ -1,23 +1,21 @@
-! RUN: %flang %s -o %t 2>&1 | FileCheck %s --allow-empty
-! CHECK-NOT: error
-
-program test_write_after_endfile
+program crash_test
   implicit none
-  integer :: n, k
-
-  open(10, status='scratch')
-
-  write(10,*) 123
-  endfile(10)
-
-  ! This should NOT crash; should go to ERR label
-  write(10, err=100, iostat=n) 456
-  k = 1
-  goto 200
-
-100 continue
-  k = 2
-
-200 continue
-  print *, "k=", k
-end program
\ No newline at end of file
+  integer :: n, l
+ 
+  open(5, iostat=n)
+  close(5, iostat=n)
+ 
+  ! Read from closed unit (expect error)
+  read(5, iostat=n) l
+ 
+  ! Write to closed unit (expect error)
+  l = 34
+  write(5, iostat=n) l
+ 
+  ! ENDFILE + REWIND
+  endfile(5, iostat=n)
+  rewind(5, iostat=n)
+ 
+  ! Critical case: WRITE after ENDFILE
+  write(5, iostat=n) l
+ 
\ No newline at end of file



More information about the flang-commits mailing list