Skip to content

Commit 9961c03

Browse files
authored
Return errc::no_such_file_or_directory in fs::access if GetFileAttributesW fails (llvm#83495)
Fixes llvm#83046 There is a race condition when calling `GetFileAttributesW` that can cause it to return `ERROR_ACCESS_DENIED` on a path which exists, which is unexpected for callers using this function to check for file existence by passing `AccessMode::Exist`. This was manifesting as a compiler crash on Windows downstream in the Swift compiler when using the `-index-store-path` flag (more information in #8224). I looked for alternate APIs to avoid bringing in `shlwapi.h`, but didn't see any good candidates. I'm not tied at all to this solution, any feedback and alternative approaches are more than welcome.
1 parent 4d03a9e commit 9961c03

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

llvm/lib/Support/Windows/Path.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@ std::error_code access(const Twine &Path, AccessMode Mode) {
623623
DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin());
624624

625625
if (Attributes == INVALID_FILE_ATTRIBUTES) {
626+
// Avoid returning unexpected error codes when querying for existence.
627+
if (Mode == AccessMode::Exist)
628+
return errc::no_such_file_or_directory;
629+
626630
// See if the file didn't actually exist.
627631
DWORD LastError = ::GetLastError();
628632
if (LastError != ERROR_FILE_NOT_FOUND && LastError != ERROR_PATH_NOT_FOUND)

0 commit comments

Comments
 (0)