9090#endif
9191
9292#include <windows.h>
93- #include <pathcch.h>
9493#include <shlwapi.h>
9594
9695#ifdef HAVE_SYS_TYPES_H
@@ -250,14 +249,42 @@ ismodule(wchar_t *filename, int update_filename)
250249 stuff as fits will be appended.
251250*/
252251
252+ static int _PathCchCombineEx_Initialized = 0 ;
253+ typedef HRESULT (__stdcall * PPathCchCombineEx ) (PWSTR pszPathOut , size_t cchPathOut ,
254+ PCWSTR pszPathIn , PCWSTR pszMore ,
255+ unsigned long dwFlags );
256+ static PPathCchCombineEx _PathCchCombineEx ;
257+
253258static void
254259join (wchar_t * buffer , const wchar_t * stuff )
255260{
256- if (FAILED (PathCchCombineEx (buffer , MAXPATHLEN + 1 , buffer , stuff , 0 ))) {
257- Py_FatalError ("buffer overflow in getpathp.c's join()" );
261+ if (_PathCchCombineEx_Initialized == 0 ) {
262+ HMODULE pathapi = LoadLibraryW (L"api-ms-win-core-path-l1-1-0.dll" );
263+ if (pathapi ) {
264+ _PathCchCombineEx = (PPathCchCombineEx )GetProcAddress (pathapi , "PathCchCombineEx" );
265+ }
266+ else {
267+ _PathCchCombineEx = NULL ;
268+ }
269+ _PathCchCombineEx_Initialized = 1 ;
270+ }
271+
272+ if (_PathCchCombineEx ) {
273+ if (FAILED (_PathCchCombineEx (buffer , MAXPATHLEN + 1 , buffer , stuff , 0 ))) {
274+ Py_FatalError ("buffer overflow in getpathp.c's join()" );
275+ }
276+ } else {
277+ if (!PathCombineW (buffer , buffer , stuff )) {
278+ Py_FatalError ("buffer overflow in getpathp.c's join()" );
279+ }
258280 }
259281}
260282
283+ static int _PathCchCanonicalizeEx_Initialized = 0 ;
284+ typedef HRESULT (__stdcall * PPathCchCanonicalizeEx ) (PWSTR pszPathOut , size_t cchPathOut ,
285+ PCWSTR pszPathIn , unsigned long dwFlags );
286+ static PPathCchCanonicalizeEx _PathCchCanonicalizeEx ;
287+
261288/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
262289 and ".." to produce a direct, well-formed path. */
263290static PyStatus
@@ -267,8 +294,26 @@ canonicalize(wchar_t *buffer, const wchar_t *path)
267294 return _PyStatus_NO_MEMORY ();
268295 }
269296
270- if (FAILED (PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , 0 ))) {
271- return INIT_ERR_BUFFER_OVERFLOW ();
297+ if (_PathCchCanonicalizeEx_Initialized == 0 ) {
298+ HMODULE pathapi = LoadLibraryW (L"api-ms-win-core-path-l1-1-0.dll" );
299+ if (pathapi ) {
300+ _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx )GetProcAddress (pathapi , "PathCchCanonicalizeEx" );
301+ }
302+ else {
303+ _PathCchCanonicalizeEx = NULL ;
304+ }
305+ _PathCchCanonicalizeEx_Initialized = 1 ;
306+ }
307+
308+ if (_PathCchCanonicalizeEx ) {
309+ if (FAILED (_PathCchCanonicalizeEx (buffer , MAXPATHLEN + 1 , path , 0 ))) {
310+ return INIT_ERR_BUFFER_OVERFLOW ();
311+ }
312+ }
313+ else {
314+ if (!PathCanonicalizeW (buffer , path )) {
315+ return INIT_ERR_BUFFER_OVERFLOW ();
316+ }
272317 }
273318 return _PyStatus_OK ();
274319}
0 commit comments