From 442841479c960aeab54a9ea2c5d361e21c0b1a61 Mon Sep 17 00:00:00 2001 From: Christopher Book Date: Wed, 11 Jul 2018 17:19:00 -0400 Subject: [PATCH 1/2] Fix descriptor leaks in win32 fstat implementation --- contrib/win32/win32compat/fileio.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index fce6bfb248b..4cba83dd340 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -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); + HANDLE dup_handle = 0; + if (!DuplicateHandle(GetCurrentProcess(), pio->handle, GetCurrentProcess(), &dup_handle, 0, + TRUE, DUPLICATE_SAME_ACCESS)) { + errno = EOTHER; + return -1; + } + + int fd = _open_osfhandle(dup_handle, 0); debug4("fstat - pio:%p", pio); if (fd == -1) { - errno = EOTHER; + CloseHandle(dup_handle); + errno = EOTHER; return -1; } - return _fstat64(fd, buf); + int res = _fstat64(fd, buf); + _close(fd); + return res; } int From 046e082c27b9c7c2e9abc751d26a9f46059f6ec2 Mon Sep 17 00:00:00 2001 From: Christopher Book Date: Thu, 12 Jul 2018 09:28:23 -0400 Subject: [PATCH 2/2] Fix spaces I added to tabs. --- contrib/win32/win32compat/fileio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index 4cba83dd340..ec55482369f 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -779,8 +779,8 @@ fileio_fstat(struct w32_io* pio, struct _stat64 *buf) int fd = _open_osfhandle(dup_handle, 0); debug4("fstat - pio:%p", pio); if (fd == -1) { - CloseHandle(dup_handle); - errno = EOTHER; + CloseHandle(dup_handle); + errno = EOTHER; return -1; }