I plan to commit the attached patch as a fix up on the tabbing with STREAM reads. Thanks to info from Harald on other compilers, this keeps gfortrans behavior is some of the cases aligned with what gfortran does now and the majority of compilers. It is handling the treatment of tabbing past the eor and then tabbing back.

Regression tested on x86_64.

Regards,

Jerry

---

libgfortran: Fix T, TL and X input positioning past the end of record

On formatted input, T and TL never moved back on a STREAM unit because
the position in the record was taken as zero there.  On all units,
positions that X, T or TR moved past the end of the record were
forgotten, so a later TL landed short, and tabbing back after reaching
the end left the end-of-record flag set, so the rest of the statement
read nothing and the next READ re-read the record.

The patch does all X, T and TL input positioning in a new read_tab
function, which measures from the left tab limit, remembers positions
past the end of the record and unreads the record terminator when
tabbing back.  A STREAM unit's format buffer is flushed at the start of
each READ so that its start is the left tab limit.

Assisted-by: Claude Opus 5.5

libgfortran/ChangeLog:

        * io/transfer.c (record_bytes_used): New function.
        (read_tab): New function.
        (formatted_transfer_scalar_read): Use them for X, TR, T and TL
        editing.
        (data_transfer_init_worker): Flush the format buffer at the start
        of a formatted STREAM read.
        (next_record): Forget positions past the end of the record.

gcc/testsuite/ChangeLog:

        * gfortran.dg/fmt_t_12.f90: New test.
        * gfortran.dg/fmt_t_13.f90: New test.
---
From d70f0407c5d759be91f22500c4c01d7b8393c41b Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Fri, 27 Nov 2026 10:36:44 -0800
Subject: [PATCH] libgfortran: Fix T, TL and X input positioning past the end
 of record

On formatted input, T and TL never moved back on a STREAM unit because
the position in the record was taken as zero there.  On all units,
positions that X, T or TR moved past the end of the record were
forgotten, so a later TL landed short, and tabbing back after reaching
the end left the end-of-record flag set, so the rest of the statement
read nothing and the next READ re-read the record.

The patch does all X, T and TL input positioning in a new read_tab
function, which measures from the left tab limit, remembers positions
past the end of the record and unreads the record terminator when
tabbing back.  A STREAM unit's format buffer is flushed at the start of
each READ so that its start is the left tab limit.

Assisted-by: Claude Opus 5.5

libgfortran/ChangeLog:

	* io/transfer.c (record_bytes_used): New function.
	(read_tab): New function.
	(formatted_transfer_scalar_read): Use them for X, TR, T and TL
	editing.
	(data_transfer_init_worker): Flush the format buffer at the start
	of a formatted STREAM read.
	(next_record): Forget positions past the end of the record.

gcc/testsuite/ChangeLog:

	* gfortran.dg/fmt_t_12.f90: New test.
	* gfortran.dg/fmt_t_13.f90: New test.
---
 gcc/testsuite/gfortran.dg/fmt_t_12.f90 |  54 +++++++++++++
 gcc/testsuite/gfortran.dg/fmt_t_13.f90 |  22 +++++
 libgfortran/io/transfer.c              | 106 +++++++++++++------------
 3 files changed, 132 insertions(+), 50 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_t_12.f90
 create mode 100644 gcc/testsuite/gfortran.dg/fmt_t_13.f90

diff --git a/gcc/testsuite/gfortran.dg/fmt_t_12.f90 b/gcc/testsuite/gfortran.dg/fmt_t_12.f90
new file mode 100644
index 00000000000..29ba3a8a16c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/fmt_t_12.f90
@@ -0,0 +1,54 @@
+! { dg-do run }
+program fmt_t_12
+  implicit none
+  character(20), parameter :: fmts(9) = [character(20) :: &
+       '(5I3,T1,I3)', '(5I3,T4,I3)', '(3I3,TL6,3I3)', '(2I3,TL6,4I3)', &
+       '(I3,TL3,5I3)', '(2I3,T30,TL27,4I3)', '(4I3,TL9,2I3)', &
+       '(I3,10X,TL10,5I3)', '(I3,T20,TL16,5I3)']
+  integer, parameter :: want(6,9) = reshape ([ &
+       1, 2, 0, 0, 0, 1,   1, 2, 0, 0, 0, 2,   1, 2, 0, 1, 2, 0, &
+       1, 2, 1, 2, 0, 0,   1, 1, 2, 0, 0, 0,   1, 2, 1, 2, 0, 0, &
+       1, 2, 0, 0, 1, 2,   1, 2, 0, 0, 0, 0,   1, 2, 0, 0, 0, 0], [6, 9])
+  character(6) :: r = '  1  2'
+  character(2) :: t
+  character(10) :: s
+  integer :: u, n(6), k, a, b, p, eol, m
+
+  do k = 1, size (fmts)
+    read (r, fmts(k)) n
+    if (any (n /= want(:,k))) stop 1
+  end do
+
+  do m = 1, 3
+    eol = merge (2, 1, m == 3)
+    t = merge (achar(10)//' ', achar(13)//achar(10), eol == 1)
+    open (newunit=u, file='fmt_t_12.dat', access='stream', &
+          form='unformatted', status='replace')
+    write (u) '  1  2'//t(1:eol)//'  3'//t(1:eol)
+    close (u)
+    open (newunit=u, file='fmt_t_12.dat', access=merge ('sequential', &
+          'stream    ', m == 1), form='formatted', status='old')
+    do k = 1, size (fmts)
+      rewind (u)
+      read (u, fmts(k)) n
+      if (any (n /= want(:,k))) stop 2
+      if (m > 1) then
+        inquire (u, pos=p)
+        if (p /= 7 + eol) stop 3
+      end if
+      read (u, '(A)') s
+      if (s /= '  3') stop 4
+    end do
+    if (m > 1) then
+      rewind (u)
+      read (u, '(I3)', advance='no') a
+      read (u, '(T1,I3)') b
+      if (a /= 1 .or. b /= 2) stop 5
+      rewind (u)
+      read (u, '(I3)', advance='no') a
+      read (u, '(T4,I3)') b
+      if (b /= 0) stop 6
+    end if
+    close (u, status='delete')
+  end do
+end program
diff --git a/gcc/testsuite/gfortran.dg/fmt_t_13.f90 b/gcc/testsuite/gfortran.dg/fmt_t_13.f90
new file mode 100644
index 00000000000..c2f3d1c1e53
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/fmt_t_13.f90
@@ -0,0 +1,22 @@
+! { dg-do run }
+program fmt_t_13
+  implicit none
+  integer :: n(6), u
+  character(6) :: r = '  1  2'
+
+  ! Padding blanks do not advance the position seen by TL.
+  read (r, '(3I3,TL6,3I3)') n
+  if (any (n /= [1, 2, 0, 1, 2, 0])) stop 1
+  read (r, '(4I3,TL9,2I3)') n
+  if (any (n /= [1, 2, 0, 0, 1, 2])) stop 2
+
+  open (newunit=u, file='fmt_t_13.dat', status='replace')
+  write (u, '(A)') r
+  rewind (u)
+  read (u, '(3I3,TL6,3I3)') n
+  if (any (n /= [1, 2, 0, 1, 2, 0])) stop 3
+  rewind (u)
+  read (u, '(4I3,TL9,2I3)') n
+  if (any (n /= [1, 2, 0, 0, 1, 2])) stop 4
+  close (u, status='delete')
+end program
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 5731aaf4dcd..0715795ca45 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -1591,6 +1591,52 @@ get_dt_format (char *p, gfc_charlen_type *length)
 }
 
 
+/* Characters transferred so far past the left tab limit, not counting a
+   record terminator already read.  A stream unit keeps no record length,
+   so use the format buffer, whose start is the left tab limit.  */
+
+static int
+record_bytes_used (st_parameter_dt *dtp)
+{
+  if (is_stream_io (dtp))
+    return (int) dtp->u.p.current_unit->fbuf->pos - dtp->u.p.sf_seen_eor;
+  return (int) (dtp->u.p.current_unit->recl
+		- dtp->u.p.current_unit->bytes_left);
+}
+
+
+/* Move the input position to character POS past the left tab limit
+   (F2023 13.8.1.2), which may lie past the end of the record.  */
+
+static void
+read_tab (st_parameter_dt *dtp, int pos)
+{
+  int used = record_bytes_used (dtp);
+
+  if (pos < used)
+    {
+      /* Moving back into the record also unreads its terminator.  */
+      int back = used - pos;
+
+      if (is_internal_unit (dtp))
+	sseek (dtp->u.p.current_unit->s, -back, SEEK_CUR);
+      else
+	{
+	  back += dtp->u.p.sf_seen_eor;
+	  fbuf_seek (dtp->u.p.current_unit, -back, SEEK_CUR);
+	}
+      dtp->u.p.sf_seen_eor = 0;
+      dtp->u.p.current_unit->bytes_left += used - pos;
+      dtp->u.p.current_unit->strm_pos -= back;
+    }
+  else if (pos > used)
+    read_x (dtp, pos - used);
+
+  used = record_bytes_used (dtp);
+  dtp->u.p.pending_spaces = pos > used ? pos - used : 0;
+}
+
+
 /* This function is in the main loop for a formatted data transfer
    statement.  It would be natural to implement this as a coroutine
    with the user program, but C makes that awkward.  We loop,
@@ -1655,11 +1701,7 @@ formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind
 
       t = f->format;
 
-      bytes_used = (int)(dtp->u.p.current_unit->recl
-		   - dtp->u.p.current_unit->bytes_left);
-
-      if (is_stream_io(dtp))
-	bytes_used = 0;
+      bytes_used = record_bytes_used (dtp) + dtp->u.p.pending_spaces;
 
       switch (t)
 	{
@@ -1894,10 +1936,7 @@ formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind
 	case FMT_X:
 	case FMT_TR:
 	  consume_data_flag = 0;
-	  dtp->u.p.skips += f->u.n;
-	  pos = bytes_used + dtp->u.p.skips - 1;
-	  dtp->u.p.pending_spaces = pos - dtp->u.p.max_pos + 1;
-	  read_x (dtp, f->u.n);
+	  read_tab (dtp, bytes_used + f->u.n);
 	  break;
 
 	case FMT_TL:
@@ -1905,18 +1944,7 @@ formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind
 	  consume_data_flag = 0;
 
 	  if (f->format == FMT_TL)
-	    {
-	      /* Handle the special case when no bytes have been used yet.
-	         Cannot go below zero. */
-	      if (bytes_used == 0)
-		{
-		  dtp->u.p.pending_spaces -= f->u.n;
-		  dtp->u.p.skips -= f->u.n;
-		  dtp->u.p.skips = dtp->u.p.skips < 0 ? 0 : dtp->u.p.skips;
-		}
-
-	      pos = bytes_used - f->u.n;
-	    }
+	    pos = bytes_used - f->u.n;
 	  else /* FMT_T */
 	    pos = f->u.n - 1;
 
@@ -1925,35 +1953,7 @@ formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind
 	     beyond the end of record because a subsequent tab could
 	     bring us back again.  */
 	  pos = pos < 0 ? 0 : pos;
-
-	  dtp->u.p.skips = dtp->u.p.skips + pos - bytes_used;
-	  dtp->u.p.pending_spaces = dtp->u.p.pending_spaces
-				    + pos - dtp->u.p.max_pos;
-	  dtp->u.p.pending_spaces = dtp->u.p.pending_spaces < 0
-				    ? 0 : dtp->u.p.pending_spaces;
-	  if (dtp->u.p.skips == 0)
-	    break;
-
-	  /* Adjust everything for end-of-record condition */
-	  if (dtp->u.p.sf_seen_eor && !is_internal_unit (dtp))
-	    {
-              dtp->u.p.current_unit->bytes_left -= dtp->u.p.sf_seen_eor;
-              dtp->u.p.skips -= dtp->u.p.sf_seen_eor;
-	      bytes_used = pos;
-	      if (dtp->u.p.pending_spaces == 0)
-		dtp->u.p.sf_seen_eor = 0;
-	    }
-	  if (dtp->u.p.skips < 0)
-	    {
-              if (is_internal_unit (dtp))
-                sseek (dtp->u.p.current_unit->s, dtp->u.p.skips, SEEK_CUR);
-              else
-                fbuf_seek (dtp->u.p.current_unit, dtp->u.p.skips, SEEK_CUR);
-	      dtp->u.p.current_unit->bytes_left -= (gfc_offset) dtp->u.p.skips;
-	      dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
-	    }
-	  else
-	    read_x (dtp, dtp->u.p.skips);
+	  read_tab (dtp, pos);
 	  break;
 
 	case FMT_S:
@@ -3733,6 +3733,11 @@ data_transfer_init_worker (st_parameter_dt *dtp, int read_flag)
      could be greater than zero from a previous non-advancing write.  */
   dtp->u.p.max_pos = dtp->u.p.current_unit->saved_pos;
 
+  /* Start the format buffer at the left tab limit (F2023 13.8.1.2).  */
+  if (read_flag && is_stream_io (dtp)
+      && dtp->u.p.current_unit->child_dtio == 0)
+    fbuf_flush (dtp->u.p.current_unit, READING);
+
   pre_position (dtp);
 
   /* Make sure that we don't do a read after a nonadvancing write.  */
@@ -4432,6 +4437,7 @@ next_record (st_parameter_dt *dtp, int done)
   else
     next_record_w (dtp, done);
 
+  dtp->u.p.pending_spaces = 0;
   fbuf_flush (dtp->u.p.current_unit, dtp->u.p.mode);
 
   if (!is_stream_io (dtp))
-- 
2.56.1

Reply via email to