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