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

via flang-commits flang-commits at lists.llvm.org
Tue Feb 24 08:53:02 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/183097.diff


2 Files Affected:

- (modified) flang/include/flang/Common/format.h (+3) 
- (modified) flang/test/Semantics/io08.f90 (+5) 


``````````diff
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

``````````

</details>


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


More information about the flang-commits mailing list