Fix descriptor leaks in win32 fstat implementation (#329)

Fix descriptor leaks in win32 fstat implementation
PowerShell/Win32-OpenSSH#1209

According to the docs for _open_osfhandle, _close will close the underlying handle:
"To close a file opened with , call _close. The underlying handle is also closed by a call to _close, so it is not necessary to call the Win32 function CloseHandle on the original handle"
This commit is contained in:
cbookg 2018-07-13 13:43:36 -04:00 committed by Manoj Ampalam
parent 8bb672aa4d
commit ce3db0ee61
1 changed files with 14 additions and 4 deletions

View File

@ -769,14 +769,24 @@ fileio_write(struct w32_io* pio, const void *buf, size_t max_bytes)
int
fileio_fstat(struct w32_io* pio, struct _stat64 *buf)
{
int fd = _open_osfhandle((intptr_t)pio->handle, 0);
debug4("fstat - pio:%p", pio);
if (fd == -1) {
HANDLE dup_handle = 0;
if (!DuplicateHandle(GetCurrentProcess(), pio->handle, GetCurrentProcess(), &dup_handle, 0,
TRUE, DUPLICATE_SAME_ACCESS)) {
errno = EOTHER;
return -1;
}
return _fstat64(fd, buf);
int fd = _open_osfhandle(dup_handle, 0);
debug4("fstat - pio:%p", pio);
if (fd == -1) {
CloseHandle(dup_handle);
errno = EOTHER;
return -1;
}
int res = _fstat64(fd, buf);
_close(fd);
return res;
}
int