For this particular case, the answer is easy enough...
There is a race condition between FileExists
and OpenFile
: what happens if the file is removed?
The only sane way to deal with this particular case is to skip FileExists
:
contents = OpenFile(file);
if (!contents) // open failed
DefaultAction();
else (SomeTest(contents))
DoSomething(contents);
contents = OpenFile(file);
if (!contents) // open failed
DefaultAction();
else (SomeTest(contents))
DoSomething(contents);
This neatly solves this problem and makes the code cleaner.
In general: Try to rethink the problem and devise another solution which avoids the issue entirely.