Skip to content

bpo-38822: Fixed os.stat failing on inaccessible directories with a trailing slash, rather than falling back to the parent directory's metadata #25527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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`.
23 changes: 21 additions & 2 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down