[Python-checkins] bpo-38822: Fixed os.stat failing on inaccessible directories. (GH-25527)
miss-islington
webhook-mailer at python.org
Thu Apr 22 16:10:05 EDT 2021
https://github.com/python/cpython/commit/8e7cebb497e6004715a5475f6b53d8ef9d30a9fa
commit: 8e7cebb497e6004715a5475f6b53d8ef9d30a9fa
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-04-22T13:09:56-07:00
summary:
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.
(cherry picked from commit fe63a401a9b3ca1751b81b5d6ddb2beb7f3675c1)
Co-authored-by: Steve Dower <steve.dower at python.org>
files:
A Misc/NEWS.d/next/Windows/2021-04-22-19-49-20.bpo-38822.jgdPmq.rst
M Modules/posixmodule.c
diff --git a/Misc/NEWS.d/next/Windows/2021-04-22-19-49-20.bpo-38822.jgdPmq.rst b/Misc/NEWS.d/next/Windows/2021-04-22-19-49-20.bpo-38822.jgdPmq.rst
new file mode 100644
index 0000000000000..072a96989c3d0
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2021-04-22-19-49-20.bpo-38822.jgdPmq.rst
@@ -0,0 +1,3 @@
+Fixed :func:`os.stat` failing on inaccessible directories with a trailing
+slash, rather than falling back to the parent directory's metadata. This
+implicitly affected :func:`os.path.exists` and :func:`os.path.isdir`.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 80f82be7be9d7..35d63188e02f7 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1799,9 +1799,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;
More information about the Python-checkins
mailing list