diff mbox series

[libgfortran] PR109358

Message ID a67380ba-be21-4d0b-918b-be46495f2316@gmail.com
State New
Headers show
Series [libgfortran] PR109358 | expand

Commit Message

Jerry D Feb. 12, 2024, 9:28 p.m. UTC
The attached patch fixes this PR by properly adjusting some variables
When using stream io. See log below. New test case included.

Regression tested on x86_64.

OK for trunk and backport?

Regards,

Jerry

ChangeLog:

     libgfortran: Adjust bytes_left and pos for access="STREAM".

     During tab edits, the pos (position) and bytes_used
     Variables were not being set correctly for stream I/O.
     Since stream I/O does not have 'real' records, the
     format buffer active length must be used instead of
     the record length variable.

     libgfortran/ChangeLog:

             PR libgfortran/109358
             * io/transfer.c (formatted_transfer_scalar_write): Adjust
             bytes_used and pos variable for stream access.

     gcc/testsuite/ChangeLog:

             PR libgfortran/109358
             * gfortran.dg/pr109358.f90: New test.

Comments

Harald Anlauf Feb. 12, 2024, 9:55 p.m. UTC | #1
Hi Jerry.

Am 12.02.24 um 22:28 schrieb Jerry D:
> The attached patch fixes this PR by properly adjusting some variables
> When using stream io. See log below. New test case included.
>
> Regression tested on x86_64.
>
> OK for trunk and backport?

the patch looks good to me.

As it is simple and very local, feel free to backport at your
discretion.

Thanks for the patch!

Harald

> Regards,
>
> Jerry
>
> ChangeLog:
>
>      libgfortran: Adjust bytes_left and pos for access="STREAM".
>
>      During tab edits, the pos (position) and bytes_used
>      Variables were not being set correctly for stream I/O.
>      Since stream I/O does not have 'real' records, the
>      format buffer active length must be used instead of
>      the record length variable.
>
>      libgfortran/ChangeLog:
>
>              PR libgfortran/109358
>              * io/transfer.c (formatted_transfer_scalar_write): Adjust
>              bytes_used and pos variable for stream access.
>
>      gcc/testsuite/ChangeLog:
>
>              PR libgfortran/109358
>              * gfortran.dg/pr109358.f90: New test.
diff mbox series

Patch

diff --git a/gcc/testsuite/gfortran.dg/pr109358.f90 b/gcc/testsuite/gfortran.dg/pr109358.f90
new file mode 100644
index 00000000000..5013984095b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr109358.f90
@@ -0,0 +1,14 @@ 
+! { dg-do run }
+! PR109358, test that tabs during stream io are correct.
+program tabs
+  implicit none
+  integer :: fd
+  character(64) :: line
+  open(newunit=fd, file="otabs.txt", form="formatted", access="stream")
+  write(fd, "(i4, t40, i4, t20, i5.5)") 1234, 5555, 67890
+  close(fd)
+  open(newunit=fd, file="otabs.txt", form="formatted")
+  read(fd,"(a)") line
+  close(fd, status='delete')
+  if (line .ne. "1234               67890               5555") stop 10
+end program tabs
diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c
index 80b60dfeb9f..99ef96a9e7c 100644
--- a/libgfortran/io/transfer.c
+++ b/libgfortran/io/transfer.c
@@ -2072,11 +2072,11 @@  formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kin
 	  dtp->u.p.skips = dtp->u.p.pending_spaces = 0;
 	}
 
-      bytes_used = dtp->u.p.current_unit->recl
-		   - dtp->u.p.current_unit->bytes_left;
-
       if (is_stream_io(dtp))
-	bytes_used = 0;
+	bytes_used = dtp->u.p.current_unit->fbuf->act;
+      else
+	bytes_used = dtp->u.p.current_unit->recl
+		    - dtp->u.p.current_unit->bytes_left;
 
       switch (t)
 	{
@@ -2452,7 +2452,11 @@  formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kin
 	  p = ((char *) p) + size;
 	}
 
-      pos = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
+      if (is_stream_io(dtp))
+	pos = dtp->u.p.current_unit->fbuf->act;
+      else
+	pos = dtp->u.p.current_unit->recl - dtp->u.p.current_unit->bytes_left;
+
       dtp->u.p.max_pos = (dtp->u.p.max_pos > pos) ? dtp->u.p.max_pos : pos;
     }