]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-86179: Avoid making case-only changes when calculating realpath() during initializ...
authorSteve Dower <steve.dower@python.org>
Thu, 14 Dec 2023 15:16:39 +0000 (15:16 +0000)
committerGitHub <noreply@github.com>
Thu, 14 Dec 2023 15:16:39 +0000 (15:16 +0000)
Modules/getpath.c

index 422056b1fb6de402f9679370de13c8c7c86979be..afa9273ebc59b74c4ea180b673506d9b8f81d7b3 100644 (file)
@@ -506,12 +506,17 @@ done:
     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);
@@ -535,7 +540,11 @@ done:
                 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);
     }