@@ -241,6 +241,36 @@ join(wchar_t *buffer, const wchar_t *stuff)
241
241
}
242
242
}
243
243
244
+ static int _PathCchCanonicalizeEx_Initialized = 0 ;
245
+ typedef HRESULT (__stdcall * PPathCchCanonicalizeEx ) (PWSTR pszPathOut , size_t cchPathOut ,
246
+ PCWSTR pszPathIn , unsigned long dwFlags );
247
+ static PPathCchCanonicalizeEx _PathCchCanonicalizeEx ;
248
+
249
+ static void canonicalize (wchar_t * buffer , const wchar_t * path )
250
+ {
251
+ if (_PathCchCanonicalizeEx_Initialized == 0 ) {
252
+ HMODULE pathapi = LoadLibraryW (L"api-ms-win-core-path-l1-1-0.dll" );
253
+ if (pathapi ) {
254
+ _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx )GetProcAddress (pathapi , "PathCchCanonicalizeEx" );
255
+ }
256
+ else {
257
+ _PathCchCanonicalizeEx = NULL ;
258
+ }
259
+ _PathCchCanonicalizeEx_Initialized = 1 ;
260
+ }
261
+
262
+ if (_PathCchCanonicalizeEx ) {
263
+ if (FAILED (_PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , 0 ))) {
264
+ Py_FatalError ("buffer overflow in getpathp.c's canonicalize()" );
265
+ }
266
+ }
267
+ else {
268
+ if (!PathCanonicalizeW (buffer , path )) {
269
+ Py_FatalError ("buffer overflow in getpathp.c's canonicalize()" );
270
+ }
271
+ }
272
+ }
273
+
244
274
/* gotlandmark only called by search_for_prefix, which ensures
245
275
'prefix' is null terminated in bounds. join() ensures
246
276
'landmark' can not overflow prefix if too long.
@@ -431,6 +461,7 @@ static void
431
461
get_progpath (void )
432
462
{
433
463
extern wchar_t * Py_GetProgramName (void );
464
+ wchar_t modulepath [MAXPATHLEN ];
434
465
wchar_t * path = _wgetenv (L"PATH" );
435
466
wchar_t * prog = Py_GetProgramName ();
436
467
@@ -443,8 +474,10 @@ get_progpath(void)
443
474
#else
444
475
dllpath [0 ] = 0 ;
445
476
#endif
446
- if (GetModuleFileNameW (NULL , progpath , MAXPATHLEN ))
477
+ if (GetModuleFileNameW (NULL , modulepath , MAXPATHLEN )) {
478
+ canonicalize (progpath , modulepath );
447
479
return ;
480
+ }
448
481
if (prog == NULL || * prog == '\0' )
449
482
prog = L"python" ;
450
483
0 commit comments