[flang-commits] [flang] [flang] Catch empty nested parenthsized format item list (PR #183097)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Feb 24 08:52:25 PST 2026


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/183097

While a completely empty format item list is meaningful -- it does nothing when there are no data items -- an empty nested parenthesized format item list is neither conforming nor useful, and will cause a runtime error if it is interpreted as unlimited repetition.  We essentially catch these cases as parsing errors when they appear in a FORMAT statement, but the format string checker used for character constants is missing them.

>From a56a2711a7cf8070cb66df3e51f243ce65305622 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 24 Feb 2026 08:43:22 -0800
Subject: [PATCH] [flang] Catch empty nested parenthsized format item list

While a completely empty format item list is meaningful --
it does nothing when there are no data items -- an empty
nested parenthesized format item list is neither conforming nor
useful, and will cause a runtime error if it is interpreted
as unlimited repetition.  We essentially catch these cases
as parsing errors when they appear in a FORMAT statement, but
the format string checker used for character constants is
missing them.
---
 flang/include/flang/Common/format.h | 3 +++
 flang/test/Semantics/io08.f90       | 5 +++++
 2 files changed, 8 insertions(+)

diff --git a/flang/include/flang/Common/format.h b/flang/include/flang/Common/format.h
index 1e64acb823616..1ddca2c706ede 100644
--- a/flang/include/flang/Common/format.h
+++ b/flang/include/flang/Common/format.h
@@ -909,6 +909,9 @@ template <typename CHAR> bool FormatValidator<CHAR>::Check() {
       if (++nestLevel > maxNesting_) {
         maxNesting_ = nestLevel;
       }
+      if (LookAheadChar() == ')') {
+        ReportError("Nested parenthesized format item list is empty");
+      }
       break;
     case TokenKind::RParen:
       if (knrValue_ >= 0) {
diff --git a/flang/test/Semantics/io08.f90 b/flang/test/Semantics/io08.f90
index 517984fe3433d..4e99ee1279fe3 100644
--- a/flang/test/Semantics/io08.f90
+++ b/flang/test/Semantics/io08.f90
@@ -40,6 +40,7 @@
   write(*,'($)')
   write(*,'(\)')
   write(*,'(RZ,RU,RP,RN,RD,RC,SS,SP,S,3G15.3e2)')
+  write(*, "()")
   write(*, '(' // achar( 9) // ')') ! horizontal tab
   write(*, '(' // achar(11) // ')') ! vertical tab
   write(*, '(' // achar(32) // ')') ! space
@@ -320,4 +321,8 @@
 
   !ERROR: Negative scale factor k (from kP) and width d in a 'E' edit descriptor must satisfy '-d < k'
   write(*, '(-4P,E20.5,E15.2)')
+
+  !ERROR: Nested parenthesized format item list is empty
+  write(*, "(I6.3, ( ))")
+
 end



More information about the flang-commits mailing list