Skip to content

Commit cb574c5

Browse files
committed
Use relative paths in UWP
1 parent 454cdd4 commit cb574c5

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

Python/dynload_win.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,20 @@ _Py_CheckPython3t(void)
278278

279279
#endif /* Py_ENABLE_SHARED */
280280

281+
static wchar_t* _Py_AbsolutePath_To_RelativePath(wchar_t* abs_path)
282+
{
283+
wchar_t* rel_path = NULL;
284+
wchar_t process_path[512] = { 0 };
285+
if (GetModuleFileNameW(NULL, process_path, 512))
286+
{
287+
wchar_t* path = wcsrchr(process_path, L'\\');
288+
path[1] = L'\0'; // strip process name
289+
if (wcsstr(abs_path, process_path))
290+
rel_path = &abs_path[wcslen(process_path)];
291+
}
292+
return rel_path;
293+
}
294+
281295
dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
282296
const char *shortname,
283297
PyObject *pathname, FILE *fp)
@@ -307,7 +321,13 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
307321

308322
Py_BEGIN_ALLOW_THREADS
309323
#ifndef MS_WINDOWS_DESKTOP
310-
hDLL = LoadPackagedLibrary(wpathname, 0);
324+
// UWP does not allow absolute paths due security restrictions.
325+
// If path is contained inside process path (sub folder), use the relative path instead.
326+
wchar_t* rel_path = _Py_AbsolutePath_To_RelativePath(wpathname);
327+
if (rel_path)
328+
hDLL = LoadPackagedLibrary(rel_path, 0);
329+
else
330+
hDLL = LoadPackagedLibrary(wpathname, 0);
311331
#else
312332
/* bpo-36085: We use LoadLibraryEx with restricted search paths
313333
to avoid DLL preloading attacks and enable use of the

0 commit comments

Comments
 (0)