Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa40f92

Browse files
izbyshevzhangyangyu
authored andcommitted
[2.7] bpo-32903: Fix a memory leak in os.chdir() on Windows (pythonGH-5801). (python#5947)
(cherry picked from commit 3e197c7) Co-authored-by: Alexey Izbyshev <[email protected]>
1 parent c20c97f commit aa40f92

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ win32_wchdir(LPCWSTR path)
984984
wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
985985
int result;
986986
wchar_t env[4] = L"=x:";
987+
int is_unc_like_path;
987988

988989
if(!SetCurrentDirectoryW(path))
989990
return FALSE;
@@ -1002,15 +1003,15 @@ win32_wchdir(LPCWSTR path)
10021003
return FALSE;
10031004
}
10041005
}
1005-
if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1006-
wcsncmp(new_path, L"//", 2) == 0)
1007-
/* UNC path, nothing to do. */
1008-
return TRUE;
1009-
env[1] = new_path[0];
1010-
result = SetEnvironmentVariableW(env, new_path);
1006+
is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1007+
wcsncmp(new_path, L"//", 2) == 0);
1008+
if (!is_unc_like_path) {
1009+
env[1] = new_path[0];
1010+
result = SetEnvironmentVariableW(env, new_path);
1011+
}
10111012
if (new_path != _new_path)
10121013
free(new_path);
1013-
return result;
1014+
return result ? TRUE : FALSE;
10141015
}
10151016
#endif
10161017

0 commit comments

Comments
 (0)