[Bug libfortran/92836] segfault with inquire()

jb at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Dec 6 16:18:00 GMT 2019


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

--- Comment #13 from Janne Blomqvist <jb at gcc dot gnu.org> ---
To clarify my previous message, instead of

inquire(..., exist=exist)
if (exist) then
   open(...)
else
   ! Handle file not existing
end if

you can do

open(..., status='old', iostat=stat)
if (stat /= o) then
   ! Handle file not existing
end if

If you open a file with status='old', then opening the file will fail if it
doesn't exist, and thus there is no race between checking whether the file
exists with inquire and trying to open it.

Similarly, if you want to make sure that the file is created, i.e. that it
doesn't exist previously, instead of doing

inquire(..., exist=exist)
if (!exist) then
    open(...)
else
    ! Handle if the file does exist
end if

You should do

open(..., status='new', iostat=stat)
if (stat /= 0) then
    ! Handle file existing
end if

With status='new', the file will be created and opened with O_CREAT|O_EXCL
which is guaranteed to be atomic.


More information about the Gcc-bugs mailing list