diff options
author | Noel Grandin <noelgrandin@collabora.co.uk> | 2024-04-29 14:31:15 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2024-04-30 11:11:38 +0200 |
commit | a7f8882e4975e4194732506e4ffb9f7af6eb9c72 (patch) | |
tree | 239afc1f6b033ead930ef0061187c6b699719f44 /sal/osl | |
parent | 1a471e674f46699a2787e3ab74353fbe1de5c456 (diff) |
convert HeapAlloc to make_unique
which means we don't have to explicitly handle OOM, and the resulting
code is much cleaner
Change-Id: I958d6678bb2d6878dda9de6bf82c5314f168db17
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166855
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sal/osl')
-rw-r--r-- | sal/osl/w32/file_dirvol.cxx | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/sal/osl/w32/file_dirvol.cxx b/sal/osl/w32/file_dirvol.cxx index 939af1d03807..7599be112a8d 100644 --- a/sal/osl/w32/file_dirvol.cxx +++ b/sal/osl/w32/file_dirvol.cxx @@ -287,23 +287,19 @@ typedef struct tagDRIVEENUM static HANDLE OpenLogicalDrivesEnum() { - LPDRIVEENUM pEnum = static_cast<LPDRIVEENUM>(HeapAlloc( GetProcessHeap(), 0, sizeof(DRIVEENUM) )); - if ( pEnum ) - { - DWORD dwNumCopied = GetLogicalDriveStringsW( SAL_N_ELEMENTS(pEnum->cBuffer) - 1, pEnum->cBuffer ); + auto xEnum = std::make_unique<DRIVEENUM>(); + DWORD dwNumCopied = GetLogicalDriveStringsW( SAL_N_ELEMENTS(xEnum->cBuffer) - 1, xEnum->cBuffer ); - if ( dwNumCopied && dwNumCopied < SAL_N_ELEMENTS(pEnum->cBuffer) ) - { - pEnum->lpCurrent = pEnum->cBuffer; - pEnum->lpIdent = L"tagDRIVEENUM"; - } - else - { - HeapFree( GetProcessHeap(), 0, pEnum ); - pEnum = nullptr; - } + if ( dwNumCopied && dwNumCopied < SAL_N_ELEMENTS(xEnum->cBuffer) ) + { + xEnum->lpCurrent = xEnum->cBuffer; + xEnum->lpIdent = L"tagDRIVEENUM"; + } + else + { + xEnum.reset(); } - return pEnum ? static_cast<HANDLE>(pEnum) : INVALID_HANDLE_VALUE; + return xEnum ? static_cast<HANDLE>(xEnum.release()) : INVALID_HANDLE_VALUE; } static bool EnumLogicalDrives(HANDLE hEnum, LPWSTR lpBuffer) @@ -334,7 +330,7 @@ static bool CloseLogicalDrivesEnum(HANDLE hEnum) if ( pEnum ) { - HeapFree( GetProcessHeap(), 0, pEnum ); + delete pEnum; fSuccess = true; } else @@ -370,20 +366,18 @@ static HANDLE OpenDirectory(const OUString& path) pos = std::copy_n(suffix.data(), suffix.length(), pos); *pos = 0; - LPDIRECTORY pDirectory = static_cast<LPDIRECTORY>(HeapAlloc(GetProcessHeap(), 0, sizeof(DIRECTORY))); - assert(pDirectory); // Don't handle OOM conditions - pDirectory->hFind = FindFirstFileW(szFileMask.get(), &pDirectory->aFirstData); + auto xDirectory = std::make_unique<DIRECTORY>(); + xDirectory->hFind = FindFirstFileW(szFileMask.get(), &xDirectory->aFirstData); - if (!IsValidHandle(pDirectory->hFind)) + if (!IsValidHandle(xDirectory->hFind)) { if ( GetLastError() != ERROR_NO_MORE_FILES ) { - HeapFree(GetProcessHeap(), 0, pDirectory); - pDirectory = nullptr; + xDirectory.reset(); } } - return static_cast<HANDLE>(pDirectory); + return static_cast<HANDLE>(xDirectory.release()); } static bool EnumDirectory(HANDLE hDirectory, LPWIN32_FIND_DATAW pFindData) @@ -430,7 +424,7 @@ static bool CloseDirectory(HANDLE hDirectory) if (IsValidHandle(pDirectory->hFind)) fSuccess = FindClose(pDirectory->hFind); - fSuccess = HeapFree(GetProcessHeap(), 0, pDirectory) && fSuccess; + delete pDirectory; } else SetLastError(ERROR_INVALID_HANDLE); |