diff options
author | Steve Dower <steve.dower@python.org> | 2021-04-22 19:45:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-22 19:45:02 (GMT) |
commit | fe63a401a9b3ca1751b81b5d6ddb2beb7f3675c1 (patch) | |
tree | 6276ea85a4f00f9a3c9c6d7637a27080287c5238 /Modules | |
parent | 2a3f4899c63806439e5bcea0c30f7e6a6295a763 (diff) | |
download | cpython-fe63a401a9b3ca1751b81b5d6ddb2beb7f3675c1.zip cpython-fe63a401a9b3ca1751b81b5d6ddb2beb7f3675c1.tar.gz cpython-fe63a401a9b3ca1751b81b5d6ddb2beb7f3675c1.tar.bz2 |
bpo-38822: Fixed os.stat failing on inaccessible directories. (GH-25527)
It would just fail if the path was inaccessible and had a trailing slash. It should fall back to the parent directory's metadata.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 8ce62c882..7a5a0e9 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1851,9 +1851,28 @@ attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *re { HANDLE hFindFile; WIN32_FIND_DATAW FileData; - hFindFile = FindFirstFileW(pszFile, &FileData); - if (hFindFile == INVALID_HANDLE_VALUE) + LPCWSTR filename = pszFile; + size_t n = wcslen(pszFile); + if (n && (pszFile[n - 1] == L'\\' || pszFile[n - 1] == L'/')) { + // cannot use PyMem_Malloc here because we do not hold the GIL + filename = (LPCWSTR)malloc((n + 1) * sizeof(filename[0])); + wcsncpy_s((LPWSTR)filename, n + 1, pszFile, n); + while (--n > 0 && (filename[n] == L'\\' || filename[n] == L'/')) { + ((LPWSTR)filename)[n] = L'\0'; + } + if (!n || filename[n] == L':') { + // Nothing left te query + free((void *)filename); + return FALSE; + } + } + hFindFile = FindFirstFileW(filename, &FileData); + if (pszFile != filename) { + free((void *)filename); + } + if (hFindFile == INVALID_HANDLE_VALUE) { return FALSE; + } FindClose(hFindFile); find_data_to_file_info(&FileData, info, reparse_tag); return TRUE; |