https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121759

            Bug ID: 121759
           Summary: Windows/MinGW: formatted reads lose decimal digits
                    after binary stream read
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: francois.depicciotto at gmail dot com
  Target Milestone: ---

Created attachment 62267
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62267&action=edit
Code to reproduce

There is a reproducible but non-deterministic bug in libgfortran under Windows
(MinGW/MinGW-w64):
after performing a binary read (or write) from a file, subsequent formatted
reads (FORM='FORMATTED') from text files sometimes lose the decimal part of
floating-point numbers.

This issue does not occur under Linux with the same gfortran version.

How to reproduce
****************

Create two files in the working directory:

toto.txt containing exactly:

123.456


toto.bin containing at least 8 zero bytes (dummy binary file)

Program (minimal reproducer):

program toto
  call readtxt
  call readtxt
  call readbin
  call readtxt
  call readtxt
end program

subroutine readtxt
  real :: t
  integer :: kf, IPB
  t = 0.0
  open(newunit=kf, file="toto.txt", form="FORMATTED",
 &     status="OLD", action="READ", iostat=IPB)
  if (IPB == 0) then
    read(kf, *) t
    print *, "t=", t
  endif
  close(kf)
end subroutine

subroutine readbin
  integer :: io, IPB
  real*8 :: x
  open(newunit=io, file="toto.bin", access="STREAM", &
       form="UNFORMATTED", status="OLD", iostat=IPB)
  read(io) x
  close(io)
end subroutine

Another code to reproduce is attached (that creates the txt itself, and with
slightly more tests).

Observed behavior
*****************

The first two calls to readtxt always read and display the correct value
(123.456).

After the binary read (readbin), the next calls to readtxt *sometimes* still
read correctly, but randomly lose the decimal part and print 123.000000.

*This happens non-deterministically: multiple executions of the same binary
produce different outcomes ; sometimes 123.456, sometimes 123.000.*

Example (Windows 11, gfortran 15.2.0, MinGW-w64 13.0.0):

 t=   123.456001
 t=   123.456001
 t=   123.000000
 t=   123.000000


On Linux with the same compiler version, the issue does not reproduce: the
value is always read correctly.

Expected behavior
*****************

Formatted reads from a text file should always return the correct
floating-point value, regardless of whether a binary read or wirte was
performed earlier.

Notes
*****

Adding DECIMAL='POINT' to the OPEN statement does not fix the issue.

Using explicit formats (e.g. read(kf,'(F15.8)') t) also does not fix it.

Performing only the OPEN/CLOSE of the binary file without an actual READ avoids
triggering the bug.

Reported by multiple users in Fortran-lang:
https://fortran-lang.discourse.group/t/problem-after-reading-binary-file-with-windows-gfortran/7407

Reply via email to