|
Boost : |
Subject: Re: [boost] [filesystem] resize_file_api leaks a HANDLE
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2015-01-02 06:34:58
On 30 Dec 2014 at 15:40, Peter Dimov wrote:
> ... when SetFilePointerEx or SetEndOfFile fails.
>
> BOOL resize_file_api(const wchar_t* p, boost::uintmax_t size)
> {
> HANDLE handle = CreateFileW(p, GENERIC_WRITE, 0, 0, OPEN_EXISTING,
> FILE_ATTRIBUTE_NORMAL, 0);
> LARGE_INTEGER sz;
> sz.QuadPart = size;
> return handle != INVALID_HANDLE_VALUE
> && ::SetFilePointerEx(handle, sz, 0, FILE_BEGIN)
> && ::SetEndOfFile(handle)
> && ::CloseHandle(handle);
> }
Additionally, that code is racy. Better is this:
// WinVista and later have the SetFileInformationByHandle() function,
but for WinXP
// compatibility we use the kernel syscall directly
static inline bool wintruncate(HANDLE h, off_t newsize)
{
windows_nt_kernel::init();
using namespace windows_nt_kernel;
IO_STATUS_BLOCK isb={ 0 };
BOOST_AFIO_ERRHNT(NtSetInformationFile(h, &isb, &newsize,
sizeof(newsize), FileEndOfFileInformation));
return true;
}
Niall
-- ned Productions Limited Consulting http://www.nedproductions.biz/ http://ie.linkedin.com/in/nialldouglas/
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk