<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72599>72599</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Unexpected behavior of file positioning statements and data transfer statements for unformatted records
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang:runtime
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          yus3710-fj
      </td>
    </tr>
</table>

<pre>
    This is an issue from Fujitsu testsuite.

The execution of a program compiled by flang-new terminates with a fatal error.

In the program, there is a `BACKSPACE` statement after some output statements for unformatted records. Then, a record is read and another output statement is executed.

The following are the test program, Flang-new and gfortran execution result.

```fortran
! test.f90
integer dim1(704),dim2(704)

do i=1,704
  dim1(i) = i
end do

rewind 4
write (4) (dim1(i),i=1,704)
write (4) (dim1(i),i=1,302)
write (4) (dim1(i),i=1,704)
backspace 4
read (4) (dim2(i),i=1,704)
write (4) (dim1(i),i=1,302)
write (4) (dim1(i),i=1,201)

rewind 4
read (4) (dim2(i),i=1,704)
read (4) (dim2(i),i=1,302)
read (4) (dim2(i),i=1,704)
read (4) (dim2(i),i=1,302)
read (4) (dim2(i),i=1,201)
print *, "ok"

end
```
```console
$ flang-new -v test.f90 
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git acef83c142619abf270145c91093c76c9d6d5ede)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12
Selected GCC installation: /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-94bdd9.o -x f95-cpp-input test.f90
 "/opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/../../../../bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /lib/../lib64/Scrt1.o /lib/../lib64/crti.o /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/crtbeginS.o -L/path/to/install/lib/clang/18/lib/aarch64-unknown-linux-gnu -L/opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12 -L/opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-94bdd9.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/rh/gcc-toolset-12/root/usr/lib/gcc/aarch64-redhat-linux/12/crtendS.o /lib/../lib64/crtn.o
$ ./a.out 

fatal Fortran runtime error(/path/to/test.f90:20): Unformatted variable-length sequential file input failed at record #3 (file offset 4036): record header has length 2816 that does not match record footer (1208)
Aborted (core dumped)
```
```console
$ gfortran -v test.f90 
Driving: gfortran -v test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.rockylinux.org/ --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.5.0 20210514 (Red Hat 8.5.0-18) (GCC)
 :
$ ./a.out 
 ok
```

It seems that information of the length of a record gets wrong.

```fortran
! test2.f90
integer dim1(704),dim2(704)

do i=1,704
  dim1(i) = i
end do

rewind 4
write (4) (dim1(i),i=1,704)
write (4) (dim1(i),i=1,302)
write (4) (dim1(i),i=1,704)
!backspace 4
!read (4) (dim2(i),i=1,704)
write (4) (dim1(i),i=1,302)
write (4) (dim1(i),i=1,201)

rewind 4
read (4) (dim2(i),i=1,704)
read (4) (dim2(i),i=1,302)
read (4) (dim2(i),i=1,704)
read (4) (dim2(i),i=1,302)
read (4) (dim2(i),i=1,201)
print *, "ok"

end
```
```console
$ flang-new test.f90 && ./a.out

fatal Fortran runtime error(/path/to/test.f90:20): Unformatted variable-length sequential file input failed at record #3 (file offset 4036): record header has length 2816 that does not match record footer (1208)
Aborted (core dumped)
$ od -i fort.4 > ng
$ flang-new test2.f90 && ./a.out
 ok
$ od -i fort.4 > ok
$ diff ok ng
429c429
< 0015300         702         703         704 2816
---
> 0015300         702         703         704 1208
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWd1v47gR_2uYlwENifLnQx68dnwtGuAOuzn0MaDJkcQNRepIKh_96wtSki1nk9wucNcW6C28jsXhfP04nBlS3HtVGcRrsvhEFvsr3oXauuuXzherPKPl16ujlS_Xd7XyoDxwA8r7DqF0toFD91UF30FAH3ynAs5ItifZtv--qxHwGUUXlDVgS-DQOls53oCwTas0Sji-QKm5qajBJwjoGmV4QA9PKtTAoeSBa0DnrLsQ_XcDocZRHGG7-OgwWQhkmX3a7v7x5Zft7oYsM_CBB2zQBOBlQAfeNgi2C20XzjQPpXXQmdK6hoeAEhwK66SfwV2NJqrgw1DU4pBL4Cb-t1H1N_LipN55lN-gUlqt7ZMyFXCHyZOI4NSdwwmUqKQqrQuOmwmcDn2nw4Vkssz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