sal/osl/w32/file_dirvol.cxx | 42 +++++++++++++++-------------------- sfx2/source/appl/shutdowniconw32.cxx | 25 ++------------------ sfx2/source/doc/syspathw32.cxx | 15 ++---------- 3 files changed, 24 insertions(+), 58 deletions(-)
New commits: commit a7f8882e4975e4194732506e4ffb9f7af6eb9c72 Author: Noel Grandin <noelgran...@collabora.co.uk> AuthorDate: Mon Apr 29 14:31:15 2024 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Tue Apr 30 11:11:38 2024 +0200 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.gran...@collabora.co.uk> 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); diff --git a/sfx2/source/appl/shutdowniconw32.cxx b/sfx2/source/appl/shutdowniconw32.cxx index a237aac13547..5d4c89307baf 100644 --- a/sfx2/source/appl/shutdowniconw32.cxx +++ b/sfx2/source/appl/shutdowniconw32.cxx @@ -658,20 +658,6 @@ void OnDrawItem(HWND /*hwnd*/, LPDRAWITEMSTRUCT lpdis) // code from setup2 project - -static void SHFree_( void *pv ) -{ - IMalloc *pMalloc; - if( NOERROR == SHGetMalloc(&pMalloc) ) - { - pMalloc->Free( pv ); - pMalloc->Release(); - } -} - -#define ALLOC(type, n) static_cast<type *>(HeapAlloc(GetProcessHeap(), 0, sizeof(type) * n )) -#define FREE(p) HeapFree(GetProcessHeap(), 0, p) - static OUString SHGetSpecialFolder( int nFolderID ) { @@ -681,14 +667,9 @@ static OUString SHGetSpecialFolder( int nFolderID ) if( hHdl == NOERROR ) { - if (WCHAR *lpFolderA = ALLOC(WCHAR, 16000)) - { - SHGetPathFromIDListW(pidl, lpFolderA); - aFolder = o3tl::toU(lpFolderA); - - FREE(lpFolderA); - SHFree_(pidl); - } + auto xFolder = std::make_unique<WCHAR[]>(16000); + SHGetPathFromIDListW(pidl, xFolder.get()); + aFolder = o3tl::toU(xFolder.get()); } return aFolder; diff --git a/sfx2/source/doc/syspathw32.cxx b/sfx2/source/doc/syspathw32.cxx index dce19e3625c0..38243e2ce7ac 100644 --- a/sfx2/source/doc/syspathw32.cxx +++ b/sfx2/source/doc/syspathw32.cxx @@ -37,19 +37,10 @@ static bool SHGetSpecialFolderW32( int nFolderID, WCHAR* pszFolder, int nSize ) if( hHdl == NOERROR ) { - if (WCHAR *lpFolder = static_cast<WCHAR*>(HeapAlloc(GetProcessHeap(), 0, 16000))) - { - SHGetPathFromIDListW( pidl, lpFolder ); - wcsncpy( pszFolder, lpFolder, nSize ); + auto xFolder = std::make_unique<WCHAR[]>(16000); - HeapFree( GetProcessHeap(), 0, lpFolder ); - IMalloc *pMalloc; - if( NOERROR == SHGetMalloc(&pMalloc) ) - { - pMalloc->Free( pidl ); - pMalloc->Release(); - } - } + SHGetPathFromIDListW( pidl, xFolder.get() ); + wcsncpy( pszFolder, xFolder.get(), nSize ); } return true; }