From 29ca914bd4fb255a6383e3d6341dcef8c7d19683 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Mon, 15 Jun 2026 08:50:15 +0900 Subject: [PATCH] [SHLWAPI] Use std::nothrow (#9150) Use preferred memory handling on memory shortage. JIRA issue: N/A - Link cppstl instead of cpprt. - Use new(std::throw) for memory allocation. - Return E_OUTOFMEMORY on allocation failure. - Don't use min macro but use __min. --- dll/win32/shlwapi/CMakeLists.txt | 2 +- dll/win32/shlwapi/policy.cpp | 14 ++------------ dll/win32/shlwapi/propbag.cpp | 27 +++++++++++++++++++++------ dll/win32/shlwapi/querysrc.cpp | 13 ++++++++++--- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/dll/win32/shlwapi/CMakeLists.txt b/dll/win32/shlwapi/CMakeLists.txt index 87301df7aa4..4d97403ee5e 100644 --- a/dll/win32/shlwapi/CMakeLists.txt +++ b/dll/win32/shlwapi/CMakeLists.txt @@ -58,7 +58,7 @@ target_compile_options(shlwapi_autocomp PRIVATE $ #include #include +#include #include WINE_DEFAULT_DEBUG_CHANNEL(policy); @@ -84,17 +85,6 @@ public: CloseHandle(m_hGlobalCounter); } - static void* operator new(size_t size) - { - // Returns NULL on failure; caller must check before use. - // NOTE: C++ UB if constructor runs on NULL, but ReactOS convention. - return LocalAlloc(LPTR, size); - } - static void operator delete(void *ptr) - { - LocalFree(ptr); - } - BOOL Initialize(const SHPOLICY_ITEM *pItems, UINT cItems) { m_nCounterValue = MINLONG; @@ -257,7 +247,7 @@ static BOOL SHPolicyCache_Create(VOID) if (g_pPolicyCache) return TRUE; - CPolicyCache *pCache = new CPolicyCache; + CPolicyCache* pCache = new(std::nothrow) CPolicyCache; if (!pCache) return FALSE; diff --git a/dll/win32/shlwapi/propbag.cpp b/dll/win32/shlwapi/propbag.cpp index fa36e083184..eca5929aa2f 100644 --- a/dll/win32/shlwapi/propbag.cpp +++ b/dll/win32/shlwapi/propbag.cpp @@ -16,6 +16,8 @@ #include // for CComVariant #include // for CA2W and CW2A #include // for StringC... functions +#include // __min +#include // std::nothrow WINE_DEFAULT_DEBUG_CHANNEL(shell); @@ -257,7 +259,9 @@ SHCreatePropertyBagOnMemory(_In_ DWORD dwMode, _In_ REFIID riid, _Out_ void **pp *ppvObj = NULL; - CComPtr pMemBag(new CMemPropertyBag(dwMode)); + CComPtr pMemBag(new(std::nothrow) CMemPropertyBag(dwMode)); + if (!pMemBag) + return E_OUTOFMEMORY; return pMemBag->QueryInterface(riid, ppvObj); } @@ -584,7 +588,9 @@ SHCreatePropertyBagOnRegKey( *ppvObj = NULL; - CComPtr pRegBag(new CRegPropertyBag(dwMode)); + CComPtr pRegBag(new(std::nothrow) CRegPropertyBag(dwMode)); + if (!pRegBag) + return E_OUTOFMEMORY; HRESULT hr = pRegBag->Init(hKey, pszSubKey); if (FAILED(hr)) @@ -836,7 +842,7 @@ CIniPropertyBag::_GetSectionAndName( if (pchSep) { UINT cchSep = (UINT)(pchSep - pszStart + 1); - StrCpyNW(pszSection, pszStart, min(cchSep, cchSectionMax)); + StrCpyNW(pszSection, pszStart, __min(cchSep, cchSectionMax)); StrCpyNW(pszName, pchSep + 1, cchNameMax); return S_OK; } @@ -995,7 +1001,9 @@ SHCreatePropertyBagOnProfileSection( if (!PathFileExistsW(lpFileName)) return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); - CComPtr pIniPB(new CIniPropertyBag(dwMode)); + CComPtr pIniPB(new(std::nothrow) CIniPropertyBag(dwMode)); + if (!pIniPB) + return E_OUTOFMEMORY; HRESULT hr = pIniPB->Init(lpFileName, pszSection); if (FAILED(hr)) @@ -1197,7 +1205,9 @@ CDesktopUpgradePropertyBag::Read( HRESULT SHGetDesktopUpgradePropertyBag(REFIID riid, void **ppvObj) { *ppvObj = NULL; - CComPtr pPropBag(new CDesktopUpgradePropertyBag()); + CComPtr pPropBag(new(std::nothrow) CDesktopUpgradePropertyBag()); + if (!pPropBag) + return E_OUTOFMEMORY; return pPropBag->QueryInterface(riid, ppvObj); } @@ -1908,7 +1918,12 @@ SHGetViewStatePropertyBag( return E_FAIL; } - CComPtr pBag(new CViewStatePropertyBag()); + CComPtr pBag(new(std::nothrow) CViewStatePropertyBag()); + if (!pBag) + { + ::LeaveCriticalSection(&g_csBagCacheLock); + return E_OUTOFMEMORY; + } hr = pBag->Init(pidl, bag_name, flags); if (FAILED(hr)) diff --git a/dll/win32/shlwapi/querysrc.cpp b/dll/win32/shlwapi/querysrc.cpp index b6872c116cc..b140e983055 100644 --- a/dll/win32/shlwapi/querysrc.cpp +++ b/dll/win32/shlwapi/querysrc.cpp @@ -10,6 +10,7 @@ #include #include #include +#include WINE_DEFAULT_DEBUG_CHANNEL(shell); @@ -305,7 +306,9 @@ STDMETHODIMP_(ULONG) CRegistrySource::Release() STDMETHODIMP CRegistrySource::EnumValues(IEnumString **ppEnum) { - CRegistryEnumValues* pEnum = new CRegistryEnumValues(); + CRegistryEnumValues* pEnum = new(std::nothrow) CRegistryEnumValues(); + if (!pEnum) + return E_OUTOFMEMORY; HRESULT hr = pEnum->Init(m_hKey, this); if (FAILED(hr)) { @@ -318,7 +321,9 @@ STDMETHODIMP CRegistrySource::EnumValues(IEnumString **ppEnum) STDMETHODIMP CRegistrySource::EnumSources(IEnumString **ppEnum) { - CRegistryEnumKeys* pEnum = new CRegistryEnumKeys(); + CRegistryEnumKeys* pEnum = new(std::nothrow) CRegistryEnumKeys(); + if (!pEnum) + return E_OUTOFMEMORY; HRESULT hr = pEnum->Init(m_hKey, this); if (FAILED(hr)) { @@ -500,7 +505,9 @@ QuerySourceCreateFromKey( { *ppv = NULL; - CRegistrySource* pRS = new CRegistrySource(); + CRegistrySource* pRS = new(std::nothrow) CRegistrySource(); + if (!pRS) + return E_OUTOFMEMORY; HRESULT hr = pRS->Init(hKey, lpSubKey, bCreate); if (SUCCEEDED(hr))