@@ -250,14 +250,43 @@ ismodule(wchar_t *filename, int update_filename)
250250 stuff as fits will be appended.
251251*/
252252
253+
254+ static int _PathCchCombineEx_Initialized = 0 ;
255+ typedef HRESULT (__stdcall * PPathCchCombineEx ) (PWSTR pszPathOut , size_t cchPathOut ,
256+ PCWSTR pszPathIn , PCWSTR pszMore ,
257+ unsigned long dwFlags );
258+ static PPathCchCombineEx _PathCchCombineEx ;
259+
253260static void
254261join (wchar_t * buffer , const wchar_t * stuff )
255262{
256- if (FAILED (PathCchCombineEx (buffer , MAXPATHLEN + 1 , buffer , stuff , 0 ))) {
257- Py_FatalError ("buffer overflow in getpathp.c's join()" );
263+ if (_PathCchCombineEx_Initialized == 0 ) {
264+ HMODULE pathapi = LoadLibraryExW (L"api-ms-win-core-path-l1-1-0.dll" , NULL ,
265+ LOAD_LIBRARY_SEARCH_SYSTEM32 );
266+ if (pathapi ) {
267+ _PathCchCombineEx = (PPathCchCombineEx )GetProcAddress (pathapi , "PathCchCombineEx" );
268+ }
269+ else {
270+ _PathCchCombineEx = NULL ;
271+ }
272+ _PathCchCombineEx_Initialized = 1 ;
273+ }
274+ if (_PathCchCombineEx ) {
275+ if (FAILED (_PathCchCombineEx (buffer , MAXPATHLEN + 1 , buffer , stuff , 0 ))) {
276+ Py_FatalError ("buffer overflow in getpathp.c's join()" );
277+ }
278+ } else {
279+ if (!PathCombineW (buffer , buffer , stuff )) {
280+ Py_FatalError ("buffer overflow in getpathp.c's join()" );
281+ }
258282 }
259283}
260284
285+ static int _PathCchCanonicalizeEx_Initialized = 0 ;
286+ typedef HRESULT (__stdcall * PPathCchCanonicalizeEx ) (PWSTR pszPathOut , size_t cchPathOut ,
287+ PCWSTR pszPathIn , unsigned long dwFlags );
288+ static PPathCchCanonicalizeEx _PathCchCanonicalizeEx ;
289+
261290/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
262291 and ".." to produce a direct, well-formed path. */
263292static PyStatus
@@ -267,8 +296,26 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
267296 return _PyStatus_NO_MEMORY ();
268297 }
269298
270- if (FAILED (PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , 0 ))) {
271- return INIT_ERR_BUFFER_OVERFLOW ();
299+ if (_PathCchCanonicalizeEx_Initialized == 0 ) {
300+ HMODULE pathapi = LoadLibraryExW (L"api-ms-win-core-path-l1-1-0.dll" , NULL ,
301+ LOAD_LIBRARY_SEARCH_SYSTEM32 );
302+ if (pathapi ) {
303+ _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx )GetProcAddress (pathapi , "PathCchCanonicalizeEx" );
304+ }
305+ else {
306+ _PathCchCanonicalizeEx = NULL ;
307+ }
308+ _PathCchCanonicalizeEx_Initialized = 1 ;
309+ }
310+ if (_PathCchCanonicalizeEx ) {
311+ if (FAILED (_PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , 0 ))) {
312+ return INIT_ERR_BUFFER_OVERFLOW ();
313+ }
314+ }
315+ else {
316+ if (!PathCanonicalizeW (buffer , path )) {
317+ return INIT_ERR_BUFFER_OVERFLOW ();
318+ }
272319 }
273320 return _PyStatus_OK ();
274321}
0 commit comments