From b82bfac4369c0429e562a834b3752e66c4821eab Mon Sep 17 00:00:00 2001 From: Mark Becwar Date: Sat, 2 Feb 2019 16:08:23 -0500 Subject: bpo-29734: nt._getfinalpathname handle leak (GH-740) Make sure that failure paths call CloseHandle outside of the function that failed --- Modules/posixmodule.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'Modules') diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 931c0d3f3a..80add7da5f 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -1640,11 +1640,6 @@ get_target_path(HANDLE hdl, wchar_t **target_path) return FALSE; } - if(!CloseHandle(hdl)) { - PyMem_RawFree(buf); - return FALSE; - } - buf[result_length] = 0; *target_path = buf; @@ -1702,9 +1697,10 @@ win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, return -1; } if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { - if (!win32_get_reparse_tag(hFile, &reparse_tag)) + if (!win32_get_reparse_tag(hFile, &reparse_tag)) { + CloseHandle(hFile); return -1; - + } /* Close the outer open file handle now that we're about to reopen it with different flags. */ if (!CloseHandle(hFile)) @@ -1721,8 +1717,14 @@ win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, if (hFile2 == INVALID_HANDLE_VALUE) return -1; - if (!get_target_path(hFile2, &target_path)) + if (!get_target_path(hFile2, &target_path)) { + CloseHandle(hFile2); return -1; + } + + if (!CloseHandle(hFile2)) { + return -1; + } code = win32_xstat_impl(target_path, result, FALSE); PyMem_RawFree(target_path); -- cgit v1.2.1