[llvm] Avoid calling `GetFileAttributesW` in Windows' `fs::access` when checking for existence (PR #83495)

Jeremy Day via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 14 10:54:10 PDT 2024


https://github.com/z2oh updated https://github.com/llvm/llvm-project/pull/83495

>From 905f9f6c1ce4e8991c472c1a6adc95f5cc8c28fd Mon Sep 17 00:00:00 2001
From: Jeremy Day <jeremy at thebrowser.company>
Date: Thu, 29 Feb 2024 11:41:42 -0800
Subject: [PATCH] Return errc::no_such_file_or_directory in fs::access if
 GetFileAttributesW fails

---
 llvm/lib/Support/Windows/Path.inc | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 3e4c1f74161c6f..344be915892799 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -623,6 +623,11 @@ 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)



More information about the llvm-commits mailing list