GeezerVison is showing github.com — gh-86179: Avoid making case-only changes when calculating realpath() … · python/cpython@a56fac8
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Modules/getpath.c +11-2Lines changed: 11 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -506,12 +506,17 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args) |
506 | 506 | HANDLE hFile; |
507 | 507 | wchar_t resolved[MAXPATHLEN+1]; |
508 | 508 | int len = 0, err; |
| 509 | + Py_ssize_t pathlen; |
509 | 510 | PyObject *result; |
510 | 511 |
|
511 | | - wchar_t *path = PyUnicode_AsWideCharString(pathobj, NULL); |
| 512 | + wchar_t *path = PyUnicode_AsWideCharString(pathobj, &pathlen); |
512 | 513 | if (!path) { |
513 | 514 | return NULL; |
514 | 515 | } |
| 516 | + if (wcslen(path) != pathlen) { |
| 517 | + PyErr_SetString(PyExc_ValueError, "path contains embedded nulls"); |
| 518 | + return NULL; |
| 519 | + } |
515 | 520 |
|
516 | 521 | Py_BEGIN_ALLOW_THREADS |
517 | 522 | 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) |
535 | 540 | len -= 4; |
536 | 541 | } |
537 | 542 | } |
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 | + } |
539 | 548 | } else { |
540 | 549 | result = Py_NewRef(pathobj); |
541 | 550 | } |
|