llvm / llvm/llvm-project

[Flang] Undetected runtime error for direct-access read of an unwritten record

Open
#193,434 1 comment 0 reactions 0 assignees View on GitHub
flang:runtime
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Flang does not detect when a direct-access READ attempts to read an unwritten record, allowing the operation to succeed silently instead of producing an I/O error.

Expected Behavior: When reading an unwritten record in a direct-access file, the READ statement should produce an I/O error, set a non-zero IOSTAT value, and trigger the ERR= branch if specified. This helps catch programming errors and prevents silent data corruption.

Actual Behavior: The READ operation succeeds without error, continuing normal execution and potentially returning garbage data.

Reproducer:
```fortran
program read_write_file
implicit none
integer, parameter :: len = 1024
integer, dimension(len) :: a
integer :: i, j, ist, ircl

inquire(iolength=ircl) (a(i),i=1,len)
open(20,file='tmp.dat',form='unformatted', access='direct',recl=ircl,status='new')
a = [ (i,i=1,len)]
do j=1,5
write(20,rec=j) (a(i),i=1,len)
end do

read(20,rec=5,iostat=ist) (a(i),i=1,len)
read(20,rec=6,err=10,iostat=ist) (a(i),i=1,len)
print '("successful read non existent record 6. this is a bug")'
goto 20
10 continue
print '("OK")'
20 close(20,status='delete')
end program
```

Contributor guide

Open the contributing guide

Research direction

Start with the supplied Fortran reproducer and run it through Flang to confirm that reading record 6 after writing records 1–5 succeeds incorrectly. Trace the direct-access READ runtime handling and its error path. Done means the read reports a non-zero IOSTAT and takes the ERR= branch for the unwritten record, while the existing record 5 read still succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.