Skip to content

[Flang] Reading byte-swapped unformatted sequential file gives incorrect record length error #57126

@jonathonpenix

Description

@jonathonpenix

A small reproducer is below:

program test_convert
  implicit none
  integer, parameter :: n = 4
  integer, parameter :: fid = 20
  real :: arr(n)
  real :: buf(n)
  integer :: i
  arr = (/1.0, 2.0, 3.0, 4.0/)

  open(fid, file="test.txt", form='UNFORMATTED', status='new')
  write(fid) arr
  close(fid)

  open(fid, file="test.txt", form='UNFORMATTED', status='old')
  read(fid) buf
  close(fid)
end program

When building with flang and running with the command FORT_CONVERT='SWAP' ./a.out, I see the following error:

fatal Fortran runtime error(./test.f90:15): Unformatted variable-length sequential file input failed at record #1 (file offset 0): hit EOF reading record with length 268435456 bytes
Aborted

When building with gfortran and running with the command GFORTRAN_CONVERT_UNIT='SWAP' ./a.out, the program executes as expected.

I think the fix is to make sure that the header and footer lengths here (and potentially other places) are also byte-swapped as appropriate, but I need to investigate further (and assuming I'm not doing something incorrect in the Fortran source).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions