See attached patch.

When a formatted STREAM read asks for more items than the record
holds, read_sf sees the end of record on the first item past it and
returns a zero length for each later item. read_block_form still added
the record terminator length to the stream position on every one of
those calls, so INQUIRE POS= ends up one byte ahead per extra item, two
with CRLF. The file offset itself is right, so only POS= and anything
positioned from it go wrong.

The fix is in read_block_form in transfer.c, plus the one-line read_x
change in read.c; the rest is the new test, which covers LF and CRLF
records.

Regression tested on x86_64.

I plan to commit this as simple and obvious.

Regards,

Jerry

---

libgfortran: [PR128107] Wrong POS after formatted STREAM read past EOR

A formatted STREAM read whose input list goes past the end of a record
left the unit position ahead by the record terminator length for each
further item, so INQUIRE POS= was wrong and a later read or write at
that position started in the wrong place.  This is because
read_block_form counted the terminator on every call after the end of
record was seen, although only the first call consumes it.  The patch
counts it only on that call, and makes read_x count it as well, since
X, TR or T editing past the end of a record consumed the terminator
without counting it.

Assisted-by: Claude Opus 5.5

        PR libfortran/128107

libgfortran/ChangeLog:

        * io/read.c (read_x): Count the record terminator in the stream
        position when it is consumed.
        * io/transfer.c (read_block_form): Count the record terminator in
        the stream position only on the read that consumes it.

gcc/testsuite/ChangeLog:

        * gfortran.dg/streamio_19.f90: New test.
>From cac9d84c65f1b830eda638eec4cf4fae9d940301 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Thu, 26 Nov 2026 09:51:06 -0800
Subject: [PATCH] libgfortran: [PR128107] Wrong POS after formatted STREAM read
 past EOR

A formatted STREAM read whose input list goes past the end of a record
left the unit position ahead by the record terminator length for each
further item, so INQUIRE POS= was wrong and a later read or write at
that position started in the wrong place.  This is because
read_block_form counted the terminator on every call after the end of
record was seen, although only the first call consumes it.  The patch
counts it only on that call, and makes read_x count it as well, since
X, TR or T editing past the end of a record consumed the terminator
without counting it.

Assisted-by: Claude Opus 5.5

	PR libfortran/128107

libgfortran/ChangeLog:

	* io/read.c (read_x): Count the record terminator in the stream
	position when it is consumed.
	* io/transfer.c (read_block_form): Count the record terminator in
	the stream position only on the read that consumes it.

gcc/testsuite/ChangeLog:

	* gfortran.dg/streamio_19.f90: New test.
---
 gcc/testsuite/gfortran.dg/streamio_19.f90 | 43 +++++++++++++++++++++++
 libgfortran/io/read.c                     |  1 +
 libgfortran/io/transfer.c                 |  5 ++-
 3 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/streamio_19.f90

diff --git a/gcc/testsuite/gfortran.dg/streamio_19.f90 b/gcc/testsuite/gfortran.dg/streamio_19.f90
new file mode 100644
index 00000000000..88a8b38c914
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/streamio_19.f90
@@ -0,0 +1,43 @@
+! { dg-do run }
+! PR 128107
+program streamio_19
+  implicit none
+  integer :: u, p, n(6), eol
+  character(10) :: s
+  character(2) :: t
+
+  do eol = 1, 2
+    t = merge (achar(10)//' ', achar(13)//achar(10), eol == 1)
+    open (newunit=u, file='streamio_19.dat', access='stream', &
+          form='unformatted', status='replace')
+    write (u) '  1  2'//t(1:eol)//'  3'//t(1:eol)//'ABCD'//t(1:eol)
+    close (u)
+
+    open (newunit=u, file='streamio_19.dat', access='stream', &
+          form='formatted', status='old')
+    read (u, '(6I3)') n
+    inquire (u, pos=p)
+    if (p /= 7 + eol) stop 1
+    read (u, '(A)', pos=p) s
+    if (s /= '  3') stop 2
+
+    rewind (u)
+    read (u, '(6I3)') n
+    read (u, '(6I3)') n
+    inquire (u, pos=p)
+    if (p /= 10 + 2*eol) stop 3
+    read (u, '(A)') s
+    if (s /= 'ABCD') stop 4
+
+    rewind (u)
+    read (u, '(2I3,10X,4I3)') n
+    inquire (u, pos=p)
+    if (p /= 7 + eol) stop 5
+
+    rewind (u)
+    read (u, '(2I3,T20,4I3)') n
+    inquire (u, pos=p)
+    if (p /= 7 + eol) stop 6
+    close (u, status='delete')
+  end do
+end program
diff --git a/libgfortran/io/read.c b/libgfortran/io/read.c
index 047fff11643..4070e09e9ef 100644
--- a/libgfortran/io/read.c
+++ b/libgfortran/io/read.c
@@ -1956,6 +1956,7 @@ read_x (st_parameter_dt *dtp, size_t n)
 	      else if (q2 != EOF) /* Oops, seek back.  */
 		fbuf_seek (dtp->u.p.current_unit, -1, SEEK_CUR);
 	    }
+	  dtp->u.p.current_unit->strm_pos += dtp->u.p.sf_seen_eor;
 	  goto done;
 	}
       n++;
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 618c7ef8b60..5731aaf4dcd 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -556,13 +556,16 @@ read_block_form (st_parameter_dt *dtp, size_t *nbytes)
       (dtp->u.p.current_unit->flags.access == ACCESS_SEQUENTIAL ||
        dtp->u.p.current_unit->flags.access == ACCESS_STREAM))
     {
+      int prev_seen_eor = dtp->u.p.sf_seen_eor;
+
       if (is_internal_unit (dtp))
 	source = read_sf_internal (dtp, nbytes);
       else
 	source = read_sf (dtp, nbytes);
 
+      /* Count the record terminator only on the read that consumed it.  */
       dtp->u.p.current_unit->strm_pos +=
-	(gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor);
+	(gfc_offset) (*nbytes + dtp->u.p.sf_seen_eor - prev_seen_eor);
       return source;
     }
 
-- 
2.56.1

Reply via email to