Skip to content
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

gh-86179: Avoid making case-only changes when calculating realpath() during initialization #113077

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions Modules/getpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,17 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
HANDLE hFile;
wchar_t resolved[MAXPATHLEN+1];
int len = 0, err;
Py_ssize_t pathlen;
PyObject *result;

wchar_t *path = PyUnicode_AsWideCharString(pathobj, NULL);
wchar_t *path = PyUnicode_AsWideCharString(pathobj, &pathlen);
if (!path) {
return NULL;
}
if (wcslen(path) != pathlen) {
PyErr_SetString(PyExc_ValueError, "path contains embedded nulls");
return NULL;
}

Py_BEGIN_ALLOW_THREADS
hFile = CreateFileW(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
Expand All @@ -535,7 +540,11 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
len -= 4;
}
}
result = PyUnicode_FromWideChar(p, len);
if (CompareStringOrdinal(path, (int)pathlen, p, len, TRUE) == CSTR_EQUAL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can pathlen be larger than INT_MAX?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, and if it is, we'll have failed earlier because the API can't handle more than 32K worth of path.

result = Py_NewRef(pathobj);
} else {
result = PyUnicode_FromWideChar(p, len);
}
} else {
result = Py_NewRef(pathobj);
}
Expand Down