3 ms·
Could you have not just checked to see if you actually created the directory and/or check to make sure you moved into the directory before proceeding with your
by Alupis 12y ago
Could you have not just checked to see if you actually created the directory and/or check to make sure you moved into the directory before proceeding with your destructive function?
(Defensive Programming 101 really).
- ColinDabritz 12y agoThey could have, but then they wouldn't have a great 'epic bug' story to share! Missing the basics is part of what leads to mistakes like this, and I'd wager that after getting bit by that they are much more careful today. Also, don't rely on implicit state (the "current directory") for a destructive command, pass the dependency in: recursively_delete_directory("/foo")
- Alupis 12y agoVery true. Reminds me of a time when I was working on a package system for an in-house linux os build and carelessly had my fakeroot directory set wrong in my configurations, which ended up treating my local root / directory as the root of the fakeroot, which is as bad as it sounds. Running the script over-wrote my entire /etc directory among other important and un-recoverable things... Gladly, it was a development system so nothing crucial was lost. Needless to say, I am way more careful today in-part due to this mishap (and hours of setting up a new dev system!)
- cperciva 12y agoThere's lots of things they could have done. As I said elsewhere, this wasn't my code,
- jwr 12y agoYou do realize that this, in turn, creates a race condition, right?
- Alupis 12y ago? not at all -- you make the deletion of the directory dependent on whether or not you tested true for the directory being present. I don't see why this would cause any race condition...
- roryokane 12y agoThe race condition is when another program is interacting with that directory too. So you check that the directory exists, and then the other program gets CPU time and uses it to delete that directory, and then your program tries to delete that directory but fails. To avoid this, you have to either get a lock on that directory somehow (opening a transaction), or you have to try deleting the directory and then check the return code or catch any exceptions to tell whether the directory existed at the time of the attempted deletion.
- jwr 12y agoIt does, because somebody might have done something to the filesystem between your test and the rest of your code. This is why I found your "Defensive Programming 101 really" comment slightly arrogant (perhaps I misunderstood the intention). Writing correct programs is not easy and one should not mock others, because there is always something new to learn.