Skip to content

Commit 3e197c7

Browse files
izbyshevzhangyangyu
authored andcommitted
bpo-32903: Fix a memory leak in os.chdir() on Windows (GH-5801)
1 parent 982c723 commit 3e197c7

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a memory leak in os.chdir() on Windows if the current directory is set
2+
to a UNC path.

Modules/posixmodule.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,15 +1529,15 @@ win32_wchdir(LPCWSTR path)
15291529
return FALSE;
15301530
}
15311531
}
1532-
if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1533-
wcsncmp(new_path, L"//", 2) == 0)
1534-
/* UNC path, nothing to do. */
1535-
return TRUE;
1536-
env[1] = new_path[0];
1537-
result = SetEnvironmentVariableW(env, new_path);
1532+
int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1533+
wcsncmp(new_path, L"//", 2) == 0);
1534+
if (!is_unc_like_path) {
1535+
env[1] = new_path[0];
1536+
result = SetEnvironmentVariableW(env, new_path);
1537+
}
15381538
if (new_path != path_buf)
15391539
PyMem_RawFree(new_path);
1540-
return result;
1540+
return result ? TRUE : FALSE;
15411541
}
15421542
#endif
15431543

0 commit comments

Comments
 (0)