]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45582: Add a NOT operator to the condition in getpath_isxfile (GH-29906)
authorneonene <53406459+neonene@users.noreply.github.com>
Fri, 3 Dec 2021 22:04:11 +0000 (07:04 +0900)
committerGitHub <noreply@github.com>
Fri, 3 Dec 2021 22:04:11 +0000 (22:04 +0000)
Modules/getpath.c

index f66fc8abbd4ffe0ecd79f57ab4fa2453dc65d958..c8c85a8540d3948087c8d2d26f2d0c0ff6cb37b3 100644 (file)
@@ -173,7 +173,9 @@ getpath_isdir(PyObject *Py_UNUSED(self), PyObject *args)
     path = PyUnicode_AsWideCharString(pathobj, NULL);
     if (path) {
 #ifdef MS_WINDOWS
-        r = (GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
+        DWORD attr = GetFileAttributesW(path);
+        r = (attr != INVALID_FILE_ATTRIBUTES) &&
+            (attr & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
 #else
         struct stat st;
         r = (_Py_wstat(path, &st) == 0) && S_ISDIR(st.st_mode) ? Py_True : Py_False;
@@ -197,7 +199,9 @@ getpath_isfile(PyObject *Py_UNUSED(self), PyObject *args)
     path = PyUnicode_AsWideCharString(pathobj, NULL);
     if (path) {
 #ifdef MS_WINDOWS
-        r = !(GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
+        DWORD attr = GetFileAttributesW(path);
+        r = (attr != INVALID_FILE_ATTRIBUTES) &&
+            !(attr & FILE_ATTRIBUTE_DIRECTORY) ? Py_True : Py_False;
 #else
         struct stat st;
         r = (_Py_wstat(path, &st) == 0) && S_ISREG(st.st_mode) ? Py_True : Py_False;
@@ -223,7 +227,9 @@ getpath_isxfile(PyObject *Py_UNUSED(self), PyObject *args)
     if (path) {
 #ifdef MS_WINDOWS
         const wchar_t *ext;
-        r = (GetFileAttributesW(path) & FILE_ATTRIBUTE_DIRECTORY) &&
+        DWORD attr = GetFileAttributesW(path);
+        r = (attr != INVALID_FILE_ATTRIBUTES) &&
+            !(attr & FILE_ATTRIBUTE_DIRECTORY) &&
             SUCCEEDED(PathCchFindExtension(path, cchPath, &ext)) &&
             (CompareStringOrdinal(ext, -1, L".exe", -1, 1 /* ignore case */) == CSTR_EQUAL)
             ? Py_True : Py_False;