llvm / llvm/llvm-project

[Flang] File specified in OPEN may be deleted after preconnection by READ to the same unit number

Open
#218,325 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Version of flang: 24.0.0git (https://github.com/llvm/llvm-project.git 4639fa213220d55af5d6824924676af02fd03bf8)

## Description
When compiling and running the following Fortran program with flang, the file specified in OPEN statement is unexpectedly deleted.
The problem occurs when a unit number is first preconnected by a READ statement without a preceding OPEN, and then the same unit number is used in an OPEN statement for an existing file.
This behavior is problematic because a user-specified existing file should not be removed unexpectedly by an OPEN operation.

## Reproducer
The following are the test program, Flang, Gfortran and ifx compilation/execution results.
In the example below:
Unit 10 is used in a READ statement without an explicit OPEN.
This appears to preconnect unit 10 to fort.10.
The same unit 10 is used in an OPEN statement with `file='input2.dat'`.
After execution, input2.dat is deleted unexpectedly.
If the first READ is preceded by an explicit OPEN statement, the problem does not occur. It also disappears when the second OPEN statement uses `ACTION='READWRITE'` instead of `ACTION='READ'`.

test.f90:

```fortran
program main
implicit none

integer :: unit_num = 10
integer :: i
character(len=100) :: line
character(len=:), allocatable :: filename

filename = 'input1.dat'
!open(unit_num, file=filename, status='old', action='read', iostat=i)
read(unit_num, iostat=i) line
print *, "read ",unit_num, i, line

filename = 'input2.dat'
open(unit_num, file=filename, status='old', action='read', iostat=i)
print *, "open input2.dat " , unit_num ,i

close(unit_num)
end program main
```

```console
$ flang test.f90; touch input2.dat; ls input2.dat; ./a.out; ls input2.dat
input2.dat
read 10 -1

open input2.dat 10 1001
ls: cannot access 'input2.dat': No such file or directory

$ gfortran test.f90; touch input2.dat; ls input2.dat; ./a.out; ls input2.dat
input2.dat
read 10 -1 Ej5N i65h)4
open input2.dat 10 0
input2.dat

$ ifx test.f90; touch input2.dat; ls input2.dat; ./a.out; ls input2.dat
input2.dat
read 10 29


open input2.dat 10 0
input2.dat
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling and running the reproducer in test.f90 with Flang, using the shown preconnection and subsequent OPEN sequence. Compare the result with gfortran and ifx; done means an existing input2.dat is not deleted and the OPEN operation succeeds as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.