See attached patch.  An E, F or G edit descriptor whose width cannot
hold the digits it asks for, such as F2.3 or E6.2, writes asterisks for
every value on output, but gfortran gave no warning at compile time.
check_format now compares the width with the narrowest output the
descriptor can produce: d+1 for F (2 when d is 0), d+e+3 for E, and for
G the E form when d is 0, otherwise F(w-n).(d-1) followed by e+2
blanks.  A labeled FORMAT is parsed before any READ or WRITE refers to
it, so its width check runs from gfc_resolve_dt the first time the
label is used for output, once per label.

The width checks are at the end of the data_desc case in check_format
in io.cc; the rest of io.cc passes format_check_kind through and keeps
the second pass from repeating the other diagnostics.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR45129] Warn on too-small E, F and G field widths

An E, F or G edit descriptor on output whose width is too small for
its digits always produces asterisks, but gfortran gave no warning at
compile time.  check_format now warns about it.

A labeled FORMAT is checked when it is matched, before a READ or
WRITE refers to it, so its I/O direction is not yet known.
check_format takes a format_check_kind: that first pass skips the
width warnings, and gfc_resolve_dt reruns the check for the width
warnings alone the first time the FORMAT is used for output, with the
other diagnostics suppressed.

Assisted-by: Claude Opus 5.5

        PR fortran/45129

gcc/fortran/ChangeLog:

        * gfortran.h (gfc_st_label): Add output_width_checked.
        * io.cc (checking_width_only): New.
        (next_char_not_space): Skip the tab warning when it is set.
        (f_min_width): New.
        (format_check_kind): New enum.
        (check_format): Add a format_check_kind argument.  Skip the width
        warnings for FMT_CHECK_DECL and the other warnings for
        FMT_CHECK_WIDTH.  Warn when an E, F or G field width on output is
        too small for its digits.
        (check_format_string): Add a format_check_kind argument.  For
        FMT_CHECK_WIDTH, suppress errors and skip the check for trailing
        characters.
        (gfc_match_format): Pass FMT_CHECK_DECL.
        (gfc_resolve_dt): Recheck a labeled FORMAT with FMT_CHECK_WIDTH
        the first time it is used for output.

gcc/testsuite/ChangeLog:

        * gfortran.dg/fmt_f0_1.f90: Expect the new warning.
        * gfortran.dg/fmt_g.f: Likewise.
        * gfortran.dg/pr45129.f90: New test.
>From 1939a68d3c17c6ac9a5a69fa5caf3d52dd5672ff Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Sat, 11 Jul 2026 15:25:02 -0700
Subject: [PATCH] fortran: [PR45129] Warn on too-small E, F and G field widths

An E, F or G edit descriptor on output whose width is too small for
its digits always produces asterisks, but gfortran gave no warning at
compile time.  check_format now warns about it.

A labeled FORMAT is checked when it is matched, before a READ or
WRITE refers to it, so its I/O direction is not yet known.
check_format takes a format_check_kind: that first pass skips the
width warnings, and gfc_resolve_dt reruns the check for the width
warnings alone the first time the FORMAT is used for output, with the
other diagnostics suppressed.

Assisted-by: Claude Opus 5.5

	PR fortran/45129

gcc/fortran/ChangeLog:

	* gfortran.h (gfc_st_label): Add output_width_checked.
	* io.cc (checking_width_only): New.
	(next_char_not_space): Skip the tab warning when it is set.
	(f_min_width): New.
	(format_check_kind): New enum.
	(check_format): Add a format_check_kind argument.  Skip the width
	warnings for FMT_CHECK_DECL and the other warnings for
	FMT_CHECK_WIDTH.  Warn when an E, F or G field width on output is
	too small for its digits.
	(check_format_string): Add a format_check_kind argument.  For
	FMT_CHECK_WIDTH, suppress errors and skip the check for trailing
	characters.
	(gfc_match_format): Pass FMT_CHECK_DECL.
	(gfc_resolve_dt): Recheck a labeled FORMAT with FMT_CHECK_WIDTH
	the first time it is used for output.

gcc/testsuite/ChangeLog:

	* gfortran.dg/fmt_f0_1.f90: Expect the new warning.
	* gfortran.dg/fmt_g.f: Likewise.
	* gfortran.dg/pr45129.f90: New test.
---
 gcc/fortran/gfortran.h                 |   3 +
 gcc/fortran/io.cc                      | 122 +++++++++++++++++++++----
 gcc/testsuite/gfortran.dg/fmt_f0_1.f90 |  12 +--
 gcc/testsuite/gfortran.dg/fmt_g.f      |  22 ++---
 gcc/testsuite/gfortran.dg/pr45129.f90  |  31 +++++++
 5 files changed, 154 insertions(+), 36 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr45129.f90

diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 49ba791c378..49f04b46e85 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -1971,6 +1971,9 @@ typedef struct gfc_st_label
 
   gfc_namespace *ns;
   int omp_region;
+
+  /* Set by gfc_resolve_dt once a FORMAT's output width warnings are done.  */
+  bool output_width_checked;
 }
 gfc_st_label;
 
diff --git a/gcc/fortran/io.cc b/gcc/fortran/io.cc
index ee73d7c118f..a558e149870 100644
--- a/gcc/fortran/io.cc
+++ b/gcc/fortran/io.cc
@@ -140,6 +140,9 @@ static enum
 { MODE_STRING, MODE_FORMAT, MODE_COPY }
 mode;
 
+/* Set by check_format_string during an FMT_CHECK_WIDTH pass.  */
+static bool checking_width_only = false;
+
 
 /* Return the next character in the format string.  */
 
@@ -206,7 +209,7 @@ next_char_not_space ()
   do
     {
       error_element = c = next_char (NONSTRING);
-      if (c == '\t')
+      if (c == '\t' && !checking_width_only)
 	gfc_warning (OPT_Wtabs, "Nonconforming tab character in format at %C");
     }
   while (gfc_is_whitespace (c));
@@ -617,13 +620,30 @@ token_to_string (format_token t)
     }
 }
 
+/* Minimum F-style field width for D fractional digits.  */
+
+static int
+f_min_width (int d)
+{
+  return d == 0 ? 2 : d + 1;
+}
+
+/* Which diagnostics check_format issues.  */
+
+enum format_check_kind
+{
+  FMT_CHECK_NORMAL,	/* All of them.  */
+  FMT_CHECK_DECL,	/* No width warnings; I/O direction unknown.  */
+  FMT_CHECK_WIDTH	/* Only the width warnings, after FMT_CHECK_DECL.  */
+};
+
 /* Check a format statement.  The format string, either from a FORMAT
    statement or a constant in an I/O statement has already been parsed
    by itself, and we are checking it for validity.  The dual origin
    means that the warning message is a little less than great.  */
 
 static bool
-check_format (bool is_input)
+check_format (bool is_input, format_check_kind kind = FMT_CHECK_NORMAL)
 {
   const char *posint_required
     = G_("Positive width required in format string at %L");
@@ -640,6 +660,7 @@ check_format (bool is_input)
   format_token t, u;
   int level;
   int repeat;
+  int value_w, value_d, value_e;
   bool rv;
 
   use_last_char = 0;
@@ -764,8 +785,9 @@ format_item_1:
 	return false;
       if (t != FMT_RPAREN || level > 0)
 	{
-	  gfc_warning (0, "$ should be the last specifier in format at %L",
-		       &format_locus);
+	  if (kind != FMT_CHECK_WIDTH)
+	    gfc_warning (0, "$ should be the last specifier in format at %L",
+			 &format_locus);
 	  goto optional_comma_1;
 	}
 
@@ -877,8 +899,9 @@ data_desc:
 	  switch (gfc_notification_std (GFC_STD_GNU))
 	    {
 	      case WARNING:
-		gfc_warning (0, "Extension: Zero width after L "
-			     "descriptor at %L", &format_locus);
+		if (kind != FMT_CHECK_WIDTH)
+		  gfc_warning (0, "Extension: Zero width after L "
+			       "descriptor at %L", &format_locus);
 		break;
 	      case ERROR:
 		gfc_error ("Extension: Zero width after L "
@@ -917,10 +940,12 @@ data_desc:
     case FMT_G:
     case FMT_EN:
     case FMT_ES:
+      value_w = value_d = 0;
+      value_e = 2;
       u = format_lex ();
       if (t == FMT_G && u == FMT_ZERO)
 	{
-	  if (is_input)
+	  if (is_input && kind != FMT_CHECK_WIDTH)
 	    {
 	      error = zero_width;
 	      goto syntax;
@@ -987,6 +1012,8 @@ data_desc:
 	    }
 	}
 
+      value_w = value;
+
       u = format_lex ();
       if (u == FMT_ERROR)
 	goto fail;
@@ -1002,7 +1029,7 @@ data_desc:
 	      saved_token = u;
               goto fail;
 	    }
-	  else
+	  else if (kind != FMT_CHECK_WIDTH)
 	    gfc_warning (0, "Period required in format "
 			 "specifier %s at %L", token_to_string (t),
 			  &format_locus);
@@ -1022,6 +1049,8 @@ data_desc:
 	  goto syntax;
 	}
 
+      value_d = value;
+
       if (t == FMT_D)
 	break;
 
@@ -1047,6 +1076,7 @@ data_desc:
 		      saved_token = u;
 		      goto fail;
 		    }
+		  value_e = 0;
 		}
 	      else
 		{
@@ -1055,8 +1085,29 @@ data_desc:
 		  goto syntax;
 		}
 	    }
+	  else
+	    value_e = value;
 	}
 
+      /* Period, D digits, exponent letter and sign, E exponent digits.  */
+      if (kind != FMT_CHECK_DECL && t == FMT_E && !is_input && value_w != 0
+	  && value_w < value_d + value_e + 3)
+	gfc_warning (0, "Field width of the %s edit descriptor at %L too "
+		     "small for the number of digits", token_to_string (t),
+		     &format_locus);
+
+      /* G with D == 0 edits like E; otherwise its narrowest output is
+	 F(w-n).(d-1) followed by n = E + 2 blanks.  */
+      if (kind != FMT_CHECK_DECL && t == FMT_G && !is_input && value_w != 0)
+	{
+	  int g_floor = (value_d == 0
+			 ? value_e + 3
+			 : value_e + 2 + f_min_width (value_d - 1));
+	  if (value_w < g_floor)
+	    gfc_warning (0, "Field width of the %s edit descriptor at %L "
+			 "too small for the number of digits",
+			 token_to_string (t), &format_locus);
+	}
       break;
 
     case FMT_DT:
@@ -1126,12 +1177,14 @@ data_desc:
 	  error = nonneg_required;
 	  goto syntax;
 	}
-      else if (is_input && t == FMT_ZERO)
+      else if (is_input && kind != FMT_CHECK_WIDTH && t == FMT_ZERO)
 	{
 	  error = posint_required;
 	  goto syntax;
 	}
 
+      value_w = value;
+
       t = format_lex ();
       if (t == FMT_ERROR)
 	goto fail;
@@ -1146,8 +1199,9 @@ data_desc:
 	    }
 	  if (mode != MODE_FORMAT)
 	    format_locus.nextc += format_string_pos;
-	  gfc_warning (0, "Period required in format specifier at %L",
-		       &format_locus);
+	  if (kind != FMT_CHECK_WIDTH)
+	    gfc_warning (0, "Period required in format specifier at %L",
+			 &format_locus);
 	  saved_token = t;
 	  break;
 	}
@@ -1161,6 +1215,12 @@ data_desc:
 	  goto syntax;
 	}
 
+      value_d = value;
+
+      if (kind != FMT_CHECK_DECL && !is_input && value_w != 0
+	  && value_w < f_min_width (value_d))
+	gfc_warning (0, "Field width of the F edit descriptor at %L too "
+		     "small for the number of digits", &format_locus);
       break;
 
     case FMT_H:
@@ -1172,8 +1232,9 @@ data_desc:
 	}
       if (mode != MODE_FORMAT)
 	format_locus.nextc += format_string_pos;
-      gfc_warning (0, "The H format specifier at %L is"
-		   " a Fortran 95 deleted feature", &format_locus);
+      if (kind != FMT_CHECK_WIDTH)
+	gfc_warning (0, "The H format specifier at %L is"
+		     " a Fortran 95 deleted feature", &format_locus);
       if (mode == MODE_STRING)
 	{
 	  format_string += value;
@@ -1208,7 +1269,7 @@ data_desc:
 	      goto syntax;
 	    }
 	}
-      else if (is_input && t == FMT_ZERO)
+      else if (is_input && kind != FMT_CHECK_WIDTH && t == FMT_ZERO)
 	{
 	  error = posint_required;
 	  goto syntax;
@@ -1361,7 +1422,8 @@ finished:
    like a format string.  */
 
 static bool
-check_format_string (gfc_expr *e, bool is_input)
+check_format_string (gfc_expr *e, bool is_input,
+		      format_check_kind kind = FMT_CHECK_NORMAL)
 {
   bool rv;
   int i;
@@ -1375,12 +1437,24 @@ check_format_string (gfc_expr *e, bool is_input)
      format string that has been calculated, but that's probably not worth the
      effort.  */
   format_locus = e->where;
-  rv = check_format (is_input);
+  /* The first pass already reported the errors and standard notes.  */
+  if (kind == FMT_CHECK_WIDTH)
+    {
+      gfc_push_suppress_errors ();
+      checking_width_only = true;
+    }
+  rv = check_format (is_input, kind);
+  if (kind == FMT_CHECK_WIDTH)
+    {
+      gfc_pop_suppress_errors ();
+      checking_width_only = false;
+    }
   /* check for extraneous characters at the end of an otherwise valid format
      string, like '(A10,I3)F5'
      start at the end and move back to the last character processed,
      spaces are OK */
-  if (rv && e->value.character.length > format_string_pos)
+  if (kind != FMT_CHECK_WIDTH
+      && rv && e->value.character.length > format_string_pos)
     for (i=e->value.character.length-1;i>format_string_pos-1;i--)
       if (e->value.character.string[i] != ' ')
         {
@@ -1433,7 +1507,7 @@ gfc_match_format (void)
 
   start = gfc_current_locus;
 
-  if (!check_format (false))
+  if (!check_format (false, FMT_CHECK_DECL))
     return MATCH_ERROR;
 
   if (gfc_match_eos () != MATCH_YES)
@@ -1456,7 +1530,7 @@ gfc_match_format (void)
   gfc_statement_label->format = e;
 
   mode = MODE_COPY;
-  check_format (false);		/* Guaranteed to succeed */
+  check_format (false, FMT_CHECK_DECL);	/* Guaranteed to succeed */
   gfc_match_eos ();		/* Guaranteed to succeed */
 
   return MATCH_YES;
@@ -3554,6 +3628,16 @@ gfc_resolve_dt (gfc_code *dt_code, gfc_dt *dt, locus *loc)
       return false;
     }
 
+  /* A labeled FORMAT's width warnings apply only to output; issue them
+     once per FORMAT.  */
+  if (k != M_READ
+      && dt->format_label && dt->format_label != &format_asterisk
+      && !dt->format_label->output_width_checked)
+    {
+      dt->format_label->output_width_checked = true;
+      check_format_string (dt->format_label->format, false, FMT_CHECK_WIDTH);
+    }
+
   return true;
 }
 
diff --git a/gcc/testsuite/gfortran.dg/fmt_f0_1.f90 b/gcc/testsuite/gfortran.dg/fmt_f0_1.f90
index cb0b2314475..8870e89003d 100644
--- a/gcc/testsuite/gfortran.dg/fmt_f0_1.f90
+++ b/gcc/testsuite/gfortran.dg/fmt_f0_1.f90
@@ -23,13 +23,13 @@
  if (str.ne."0.") STOP 8
  write (str,'(F0.0)') 0.1
  if (str.ne."0.") STOP 9
- write (str,'(F1.0)') -0.0
+ write (str,'(F1.0)') -0.0 ! { dg-warning "Field width" }
  if (str.ne."*") STOP 10
- write (str,'(F1.0)') 0.001
+ write (str,'(F1.0)') 0.001 ! { dg-warning "Field width" }
  if (str.ne."*") STOP 11
- write (str,'(F1.0)') 0.01
+ write (str,'(F1.0)') 0.01 ! { dg-warning "Field width" }
  if (str.ne."*") STOP 12
- write (str,'(F1.0)') 0.1
+ write (str,'(F1.0)') 0.1 ! { dg-warning "Field width" }
  if (str.ne."*") STOP 13
  write (str,'(F2.0)') -0.001
  if (str.ne."**") STOP 14
@@ -51,7 +51,7 @@
  if (str.ne."-0.") STOP 22
  write (str,'(F2.0)') -0.0
  if (str.ne."**") STOP 23
- write (str,'(F1.0)') -0.0
+ write (str,'(F1.0)') -0.0 ! { dg-warning "Field width" }
  if (str.ne."*") STOP 24
  write (str,'(F0.1)') -0.0
  if (str.ne."-.0") STOP 25
@@ -59,6 +59,6 @@
  if (str.ne."-.0") STOP 26
  write (str,'(F2.1)') -0.0
  if (str.ne."**") STOP 27
- write (str,'(F1.1)') -0.0
+ write (str,'(F1.1)') -0.0 ! { dg-warning "Field width" }
  if (str.ne."*") STOP 28
  END  
diff --git a/gcc/testsuite/gfortran.dg/fmt_g.f b/gcc/testsuite/gfortran.dg/fmt_g.f
index 7ed9348a60f..0df5fa1b4d0 100644
--- a/gcc/testsuite/gfortran.dg/fmt_g.f
+++ b/gcc/testsuite/gfortran.dg/fmt_g.f
@@ -6,27 +6,27 @@
 
        WRITE(buffer,"(G0.5,'<')") -10000.
        if (buffer.ne."-10000.<") STOP 1
-       WRITE(buffer,"(G1.5E5,'<')") -10000.
+       WRITE(buffer,"(G1.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."*<") STOP 2
-       WRITE(buffer,"(G2.5E5,'<')") -10000.
+       WRITE(buffer,"(G2.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."**<") STOP 3
-       WRITE(buffer,"(G3.5E5,'<')") -10000.
+       WRITE(buffer,"(G3.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."***<") STOP 4
-       WRITE(buffer,"(G4.5E5,'<')") -10000.
+       WRITE(buffer,"(G4.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."****<") STOP 5
-       WRITE(buffer,"(G5.5E5,'<')") -10000.
+       WRITE(buffer,"(G5.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."*****<") STOP 6
-       WRITE(buffer,"(G6.5E5,'<')") -10000.
+       WRITE(buffer,"(G6.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."******<") STOP 7
-       WRITE(buffer,"(G7.5E5,'<')") -10000.
+       WRITE(buffer,"(G7.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."*******<") STOP 8
-       WRITE(buffer,"(G8.5E5,'<')") -10000.
+       WRITE(buffer,"(G8.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."********<") STOP 9
-       WRITE(buffer,"(G9.5E5,'<')") -10000.
+       WRITE(buffer,"(G9.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."*********<") STOP 10
-       WRITE(buffer,"(G10.5E5,'<')") -10000.
+       WRITE(buffer,"(G10.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."**********<") STOP 11
-       WRITE(buffer,"(G11.5E5,'<')") -10000.
+       WRITE(buffer,"(G11.5E5,'<')") -10000.  ! { dg-warning "Field width" }
        if (buffer.ne."***********<") STOP 12
        WRITE(buffer,"(G12.5E5,'<')") -10000.
        if (buffer.ne."************<") STOP 13
diff --git a/gcc/testsuite/gfortran.dg/pr45129.f90 b/gcc/testsuite/gfortran.dg/pr45129.f90
new file mode 100644
index 00000000000..1264c8cf93f
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr45129.f90
@@ -0,0 +1,31 @@
+! { dg-do compile }
+!
+! PR 45129
+
+program p
+  real :: x = 1.0
+  character(20) :: s
+100 format (F2.3)   ! { dg-warning "Field width of the F edit descriptor" }
+200 format (E5.3)   ! { dg-warning "Field width of the E edit descriptor" }
+300 format (G4.2)   ! { dg-warning "Field width of the G edit descriptor" }
+400 format (F1.0)
+500 format (F4.2)
+  read (s, 400) x
+  read (s, 100) x
+  write (s, 100) x
+  write (s, 100) x
+  print 200, x
+  write (s, 300) x
+  write (s, 500) x
+  read (s, '(F1.0)') x
+  write (s, '(F1.0)') x      ! { dg-warning "Field width of the F edit descriptor" }
+  write (s, '(F2.0)') x
+  write (s, '(F2.2)') x      ! { dg-warning "Field width of the F edit descriptor" }
+  write (s, '(F3.2)') x
+  write (s, '(E6.2)') x      ! { dg-warning "Field width of the E edit descriptor" }
+  write (s, '(E7.2)') x
+  write (s, '(E7.2E3)') x    ! { dg-warning "Field width of the E edit descriptor" }
+  write (s, '(E8.2E3)') x
+  write (s, '(G5.2)') x      ! { dg-warning "Field width of the G edit descriptor" }
+  write (s, '(G6.2)') x
+end program
-- 
2.56.1

Reply via email to