The Daily WTF
Follow
CodeSOD: I Exist
Greta, working in an ancient Pascal development environment, encountered a bug where her program reported files as non-existent despite their presence. She traced the issue to the system library's FileExists function. This function relies on FileAge, which retrieves a file's last modified timestamp. FileAge, in turn, uses FileTimeToDosDateTime to convert time formats. The critical flaw lies in FileAge's handling of FileTimeToDosDateTime's return value. FileTimeToDosDateTime returns a boolean to indicate success or failure, and sets an error code upon failure. However, FileAge does not check this boolean return value. If FileTimeToDosDateTime fails, FileAge proceeds to return -1. Consequently, FileExists interprets this -1 as the file not existing. The root cause of Greta's specific bug was that the process writing the files was not setting the "last write time." Without a valid last write time, FileTimeToDosDateTime fails, causing FileAge to return -1, and thus FileExists incorrectly reports the file as absent. This highlights a common issue in system libraries where subtle errors in error handling lead to significant functional bugs.