Skip to content

Commit

Permalink
gh-86179: Avoid making case-only changes when calculating realpath() …
Browse files Browse the repository at this point in the history
…during initialization (GH-113077)
  • Loading branch information
zooba committed Dec 14, 2023
1 parent 6873555 commit fd81afc
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Modules/getpath.c
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) {
result = Py_NewRef(pathobj);
} else {
result = PyUnicode_FromWideChar(p, len);
}
} else {
result = Py_NewRef(pathobj);
}
Expand Down

0 comments on commit fd81afc

Please sign in to comment.