Skip to content

Commit ecaf5fd

Browse files
zoobaGlyphack
authored andcommitted
pythongh-86179: Avoid making case-only changes when calculating realpath() during initialization (pythonGH-113077)
1 parent 30d545d commit ecaf5fd

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Modules/getpath.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,17 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
506506
HANDLE hFile;
507507
wchar_t resolved[MAXPATHLEN+1];
508508
int len = 0, err;
509+
Py_ssize_t pathlen;
509510
PyObject *result;
510511

511-
wchar_t *path = PyUnicode_AsWideCharString(pathobj, NULL);
512+
wchar_t *path = PyUnicode_AsWideCharString(pathobj, &pathlen);
512513
if (!path) {
513514
return NULL;
514515
}
516+
if (wcslen(path) != pathlen) {
517+
PyErr_SetString(PyExc_ValueError, "path contains embedded nulls");
518+
return NULL;
519+
}
515520

516521
Py_BEGIN_ALLOW_THREADS
517522
hFile = CreateFileW(path, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
@@ -535,7 +540,11 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
535540
len -= 4;
536541
}
537542
}
538-
result = PyUnicode_FromWideChar(p, len);
543+
if (CompareStringOrdinal(path, (int)pathlen, p, len, TRUE) == CSTR_EQUAL) {
544+
result = Py_NewRef(pathobj);
545+
} else {
546+
result = PyUnicode_FromWideChar(p, len);
547+
}
539548
} else {
540549
result = Py_NewRef(pathobj);
541550
}

0 commit comments

Comments
 (0)