From aa40f92240adea7067c3add8e09cec09dcf24d7f Mon Sep 17 00:00:00 2001 From: Alexey Izbyshev Date: Thu, 1 Mar 2018 13:27:34 +0300 Subject: [PATCH] [2.7] bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801). (#5947) (cherry picked from commit 3e197c7a6740d564ad52fb7901c07d5ff49460f5) Co-authored-by: Alexey Izbyshev --- .../2018-02-28-11-03-24.bpo-32903.1SXY4t.rst | 2 ++ Modules/posixmodule.c | 15 ++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Windows/2018-02-28-11-03-24.bpo-32903.1SXY4t.rst diff --git a/Misc/NEWS.d/next/Windows/2018-02-28-11-03-24.bpo-32903.1SXY4t.rst b/Misc/NEWS.d/next/Windows/2018-02-28-11-03-24.bpo-32903.1SXY4t.rst new file mode 100644 index 00000000000..a20a414790f --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2018-02-28-11-03-24.bpo-32903.1SXY4t.rst @@ -0,0 +1,2 @@ +Fix a memory leak in os.chdir() on Windows if the current directory is set +to a UNC path. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index f8e081ed7d6..2baf9202c30 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -984,6 +984,7 @@ win32_wchdir(LPCWSTR path) wchar_t _new_path[MAX_PATH+1], *new_path = _new_path; int result; wchar_t env[4] = L"=x:"; + int is_unc_like_path; if(!SetCurrentDirectoryW(path)) return FALSE; @@ -1002,15 +1003,15 @@ win32_wchdir(LPCWSTR path) return FALSE; } } - if (wcsncmp(new_path, L"\\\\", 2) == 0 || - wcsncmp(new_path, L"//", 2) == 0) - /* UNC path, nothing to do. */ - return TRUE; - env[1] = new_path[0]; - result = SetEnvironmentVariableW(env, new_path); + is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || + wcsncmp(new_path, L"//", 2) == 0); + if (!is_unc_like_path) { + env[1] = new_path[0]; + result = SetEnvironmentVariableW(env, new_path); + } if (new_path != _new_path) free(new_path); - return result; + return result ? TRUE : FALSE; } #endif