Quoth the article: Say for instance that you’re reading user settings from a file, but the file may not exist since the user may not have started the application before. The wrong thing to do here is to try to open the file, catch any exceptions and move on. The right thing to do is to check whether the file exists before trying to open it.
No. The right thing to do is to try to open the file, and if you fail, check if errno == ENOENT and handle it appropriately. If you check to see if the file exists before (or after!) trying to open it, you have a race condition which is at best liable to produce screwy results and at worst a serious security flaw.
And if you're using a language which doesn't allow you to look at errno to figure out why open(2) failed... start using a real programming language.
1 comment
[ 3.0 ms ] story [ 14.8 ms ] threadNo. The right thing to do is to try to open the file, and if you fail, check if errno == ENOENT and handle it appropriately. If you check to see if the file exists before (or after!) trying to open it, you have a race condition which is at best liable to produce screwy results and at worst a serious security flaw.
And if you're using a language which doesn't allow you to look at errno to figure out why open(2) failed... start using a real programming language.