https://gcc.gnu.org/g:21e0a742e7b70dcb9d8bed87a7d1b1b77b48b364

commit r15-9755-g21e0a742e7b70dcb9d8bed87a7d1b1b77b48b364
Author: Jerry DeLisle <jvdeli...@gcc.gnu.org>
Date:   Sat May 31 08:57:22 2025 -0700

    Fortran: Fix handling of parsed format strings.
    
            Previously parsed strings with errors were being cached such
            that subsequent use of the format string were not being
            checked for errors.
    
            PR libfortran/119856
    
    libgfortran/ChangeLog:
    
            * io/format.c (parse_format_list): Set the fmt->error
            message for missing comma.
            (parse_format): Do not cache the parsed format string
            if a previous error ocurred.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/pr119856.f90: New test.
    
    (cherry picked from commit 5ff48aabf76c8913c013f233d3f42bb217a16e7b)

Diff:
---
 gcc/testsuite/gfortran.dg/pr119856.f90 | 15 +++++++++++++++
 libgfortran/io/format.c                | 10 +++++-----
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/gfortran.dg/pr119856.f90 
b/gcc/testsuite/gfortran.dg/pr119856.f90
new file mode 100644
index 000000000000..60ada0a910e5
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr119856.f90
@@ -0,0 +1,15 @@
+! { dg-do run }
+! PR119856, the error should occur in both write statements.
+program badfmt
+  implicit none
+
+  character(10):: fmt = "(AI5)"  ! Not a PARAMETER so not examined
+                                 ! at compile time
+  integer :: ioerr
+  ioerr = 0
+  write (*, fmt, iostat=ioerr) 'value =', 42
+  if (ioerr /= 5006) stop 10
+!
+  write (*, fmt, iostat=ioerr) 'value =', 43
+  if (ioerr /= 5006) stop 13
+end program badfmt
diff --git a/libgfortran/io/format.c b/libgfortran/io/format.c
index eef1d34853e7..87e21a9a4327 100644
--- a/libgfortran/io/format.c
+++ b/libgfortran/io/format.c
@@ -1235,9 +1235,9 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
 
     default:
       /* Assume a missing comma with -std=legacy, GNU extension. */
-      if (compile_options.warn_std == 0)
-       goto format_item_1;
-      format_error (dtp, tail, comma_missing);
+      if (compile_options.warn_std != 0)
+       fmt->error = comma_missing;
+      goto format_item_1;
     }
 
   /* Optional comma is a weird between state where we've just finished
@@ -1252,7 +1252,7 @@ parse_format_list (st_parameter_dt *dtp, bool *seen_dd)
     case FMT_RPAREN:
       goto finished;
 
-    default:                   /* Assume that we have another format item */
+    default:   /* Assume that we have another format item */
       fmt->saved_token = t;
       break;
     }
@@ -1419,7 +1419,7 @@ parse_format (st_parameter_dt *dtp)
   else
     fmt->error = "Missing initial left parenthesis in format";
 
-  if (format_cache_ok)
+  if (format_cache_ok && !fmt->error)
     save_parsed_format (dtp);
   else
     dtp->u.p.format_not_saved = 1;

Reply via email to