Skip to content

Commit 5146877

Browse files
authored
bpo-45375: Fix assertion failure due to searching for stdlib in unnormalised paths (GH-28735)
1 parent de4052f commit 5146877

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixes an assertion failure due to searching for the standard library in
2+
unnormalised paths.

PC/getpathp.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,21 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
265265
return _PyStatus_NO_MEMORY();
266266
}
267267

268-
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
268+
if (PathIsRelativeW(path)) {
269+
wchar_t buff[MAXPATHLEN];
270+
if (!GetCurrentDirectoryW(MAXPATHLEN, buff)) {
271+
return _PyStatus_ERR("unable to find current working directory");
272+
}
273+
if (FAILED(PathCchCombineEx(buff, MAXPATHLEN + 1, buff, path, PATHCCH_ALLOW_LONG_PATHS))) {
274+
return INIT_ERR_BUFFER_OVERFLOW();
275+
}
276+
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, buff, PATHCCH_ALLOW_LONG_PATHS))) {
277+
return INIT_ERR_BUFFER_OVERFLOW();
278+
}
279+
return _PyStatus_OK();
280+
}
281+
282+
if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, PATHCCH_ALLOW_LONG_PATHS))) {
269283
return INIT_ERR_BUFFER_OVERFLOW();
270284
}
271285
return _PyStatus_OK();
@@ -291,6 +305,9 @@ search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path)
291305
/* Search from argv0_path, until LANDMARK is found.
292306
We guarantee 'prefix' is null terminated in bounds. */
293307
wcscpy_s(prefix, MAXPATHLEN+1, argv0_path);
308+
if (!prefix[0]) {
309+
return 0;
310+
}
294311
wchar_t stdlibdir[MAXPATHLEN+1];
295312
wcscpy_s(stdlibdir, Py_ARRAY_LENGTH(stdlibdir), prefix);
296313
/* We initialize with the longest possible path, in case it doesn't fit.
@@ -925,6 +942,7 @@ calculate_module_search_path(PyCalculatePath *calculate,
925942
the parent of that.
926943
*/
927944
if (prefix[0] == L'\0') {
945+
PyStatus status;
928946
wchar_t lookBuf[MAXPATHLEN+1];
929947
const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
930948
while (1) {
@@ -939,6 +957,10 @@ calculate_module_search_path(PyCalculatePath *calculate,
939957
nchars = lookEnd-look;
940958
wcsncpy(lookBuf, look+1, nchars);
941959
lookBuf[nchars] = L'\0';
960+
status = canonicalize(lookBuf, lookBuf);
961+
if (_PyStatus_EXCEPTION(status)) {
962+
return status;
963+
}
942964
/* Up one level to the parent */
943965
reduce(lookBuf);
944966
if (search_for_prefix(prefix, lookBuf)) {

PCbuild/regen.targets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@
104104
Condition="($(Platform) == 'Win32' or $(Platform) == 'x64') and
105105
$(Configuration) != 'PGInstrument' and $(Configuration) != 'PGUpdate'">
106106
<Message Text="Regenerate @(_TestFrozenOutputs->'%(Filename)%(Extension)', ' ')" Importance="high" />
107-
<Exec Command="$(PythonExe) Programs\freeze_test_frozenmain.py Programs/test_frozenmain.h"
107+
<Exec Command='setlocal
108+
set PYTHONPATH=$(PySourcePath)Lib
109+
"$(PythonExe)" Programs\freeze_test_frozenmain.py Programs\test_frozenmain.h'
108110
WorkingDirectory="$(PySourcePath)" />
109111
</Target>
110112

0 commit comments

Comments
 (0)