[Python-checkins] bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801)

Xiang Zhang webhook-mailer at python.org
Thu Mar 1 04:14:05 EST 2018


https://github.com/python/cpython/commit/3e197c7a6740d564ad52fb7901c07d5ff49460f5
commit: 3e197c7a6740d564ad52fb7901c07d5ff49460f5
branch: master
author: Alexey Izbyshev <izbyshev at users.noreply.github.com>
committer: Xiang Zhang <angwerzx at 126.com>
date: 2018-03-01T17:13:56+08:00
summary:

bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801)

files:
A Misc/NEWS.d/next/Windows/2018-02-28-11-03-24.bpo-32903.1SXY4t.rst
M Modules/posixmodule.c

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 000000000000..a20a414790f8
--- /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 4b592298834d..6bba8ee26e16 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1529,15 +1529,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
 



More information about the Python-checkins mailing list