Skip to content

Commit

Permalink
Return errc::no_such_file_or_directory in fs::access if `GetFileA…
Browse files Browse the repository at this point in the history
…ttributesW` 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
swiftlang#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.
  • Loading branch information
z2oh authored Mar 27, 2024
1 parent 4d03a9e commit 9961c03
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions llvm/lib/Support/Windows/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ std::error_code access(const Twine &Path, AccessMode Mode) {
DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin());

if (Attributes == INVALID_FILE_ATTRIBUTES) {
// Avoid returning unexpected error codes when querying for existence.
if (Mode == AccessMode::Exist)
return errc::no_such_file_or_directory;

// See if the file didn't actually exist.
DWORD LastError = ::GetLastError();
if (LastError != ERROR_FILE_NOT_FOUND && LastError != ERROR_PATH_NOT_FOUND)
Expand Down

0 comments on commit 9961c03

Please sign in to comment.