[Bug libfortran/92836] segfault with inquire()

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


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

--- Comment #9 from Janne Blomqvist <jb at gcc dot gnu.org> ---
(In reply to Thomas Koenig from comment #8)
> > No. The inquire() is used only to see if the file exists already. If it
> > does, the code branches to read the file, if it does not, the code branches
> > to generate the data for the file and then write it.
> 
> You have to watch out here.
> 
> If you are
> 
> a) using INQUIRE to check if the file exists
> 
> b) using OPEN if it does not
> 
> in a thread, this is classic race condition - separate threads
> may INQUIRE and then try to open, in parallel. This is not atomic,
> and is a classic race condition.
> 
> You should always put such a sequence into OMP CRITICAL.  This has
> the nice side effect that you will also avoid this bug (which should
> be fixed regardless).
> 
> Also, why do you use inquire at all? AFAIK, it is not an error
> to OPEN a file more than one if you don't change anything, so
> a simple
> 
>   open (10,file="foo.dat",status="unknown")
> 
> should, in principle, work even with OpenMP; if it doesn't, this is
> another bug that needs fixing.

The best way in cases like this is usually to skip the inquire, try to open the
file with a suitable value for status=, and if the open fails handle the error.
That avoids the race condition.

That being said, like you say, this is still a case that GFortran should handle
without crashing, so yes it's a bug that should be fixed.


More information about the Gcc-bugs mailing list