]> granicus.if.org Git - python/commitdiff
bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801)
authorAlexey Izbyshev <izbyshev@users.noreply.github.com>
Thu, 1 Mar 2018 09:13:56 +0000 (12:13 +0300)
committerXiang Zhang <angwerzx@126.com>
Thu, 1 Mar 2018 09:13:56 +0000 (17:13 +0800)
Misc/NEWS.d/next/Windows/2018-02-28-11-03-24.bpo-32903.1SXY4t.rst [new file with mode: 0644]
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 (file)
index 0000000..a20a414
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a memory leak in os.chdir() on Windows if the current directory is set
+to a UNC path.
index 4b592298834de86a85af3115251b03e27b1174f6..6bba8ee26e167014e1318cd0cb5ef89169b615c8 100644 (file)
@@ -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