diff options
author | Steve Dower <steve.dower@microsoft.com> | 2018-02-22 20:33:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-22 20:33:16 (GMT) |
commit | 1d3c518c5ecbd78478738f068f4f035f81f035f9 (patch) | |
tree | 9fa806636df61b86e6c40191ba8257c6ad39c525 /PC | |
parent | 6eab93cfe5ee08a6168e5bb69474e461cc7ac535 (diff) | |
download | cpython-1d3c518c5ecbd78478738f068f4f035f81f035f9.zip cpython-1d3c518c5ecbd78478738f068f4f035f81f035f9.tar.gz cpython-1d3c518c5ecbd78478738f068f4f035f81f035f9.tar.bz2 |
bpo-32457: Improves handling of denormalized executable path when launching Python (GH-5756) (#5818)
Diffstat (limited to 'PC')
-rw-r--r-- | PC/getpathp.c | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/PC/getpathp.c b/PC/getpathp.c index e7be704..af4af76 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -241,6 +241,36 @@ join(wchar_t *buffer, const wchar_t *stuff) } } +static int _PathCchCanonicalizeEx_Initialized = 0; +typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut, + PCWSTR pszPathIn, unsigned long dwFlags); +static PPathCchCanonicalizeEx _PathCchCanonicalizeEx; + +static void canonicalize(wchar_t *buffer, const wchar_t *path) +{ + if (_PathCchCanonicalizeEx_Initialized == 0) { + HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll"); + if (pathapi) { + _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx"); + } + else { + _PathCchCanonicalizeEx = NULL; + } + _PathCchCanonicalizeEx_Initialized = 1; + } + + if (_PathCchCanonicalizeEx) { + if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) { + Py_FatalError("buffer overflow in getpathp.c's canonicalize()"); + } + } + else { + if (!PathCanonicalizeW(buffer, path)) { + Py_FatalError("buffer overflow in getpathp.c's canonicalize()"); + } + } +} + /* gotlandmark only called by search_for_prefix, which ensures 'prefix' is null terminated in bounds. join() ensures 'landmark' can not overflow prefix if too long. @@ -431,6 +461,7 @@ static void get_progpath(void) { extern wchar_t *Py_GetProgramName(void); + wchar_t modulepath[MAXPATHLEN]; wchar_t *path = _wgetenv(L"PATH"); wchar_t *prog = Py_GetProgramName(); @@ -443,8 +474,10 @@ get_progpath(void) #else dllpath[0] = 0; #endif - if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN)) + if (GetModuleFileNameW(NULL, modulepath, MAXPATHLEN)) { + canonicalize(progpath, modulepath); return; + } if (prog == NULL || *prog == '\0') prog = L"python"; |