From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Thu, 1 Mar 2018 10:28:20 +0000 (-0800) Subject: bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801) (#5946) X-Git-Tag: v3.6.5rc1~27 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=10fb1bf7766e7eb9500d328ddd1035e3f823fb57;p=python bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801) (#5946) (cherry picked from commit 3e197c7a6740d564ad52fb7901c07d5ff49460f5) Co-authored-by: Alexey Izbyshev --- 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 0000000000..a20a414790 --- /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 885c2674e3..0837a4a499 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1444,15 +1444,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); + int 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 != path_buf) PyMem_RawFree(new_path); - return result; + return result ? TRUE : FALSE; } #endif