Skip to content

Commit 29ca914

Browse files
authored
[SHLWAPI] Use <new> std::nothrow (reactos#9150)
Use preferred memory handling on memory shortage. JIRA issue: N/A - Link cppstl instead of cpprt. - Use <new> new(std::throw) for memory allocation. - Return E_OUTOFMEMORY on allocation failure. - Don't use <stdlib.h> min macro but use <cstdlib> __min.
1 parent 974ae07 commit 29ca914

4 files changed

Lines changed: 34 additions & 22 deletions

File tree

‎dll/win32/shlwapi/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ target_compile_options(shlwapi_autocomp PRIVATE $<TARGET_PROPERTY:shlwapi,COMPIL
5858
add_dependencies(shlwapi_autocomp psdk)
5959

6060
set_module_type(shlwapi win32dll UNICODE)
61-
target_link_libraries(shlwapi uuid wine cpprt)
61+
target_link_libraries(shlwapi uuid wine cppstl)
6262
add_delay_importlibs(shlwapi userenv oleaut32 ole32 comctl32 comdlg32 mpr mlang urlmon shell32 winmm version)
6363
add_importlibs(shlwapi user32 gdi32 advapi32 wininet msvcrt kernel32 ntdll)
6464
add_pch(shlwapi precomp.h "${PCH_SKIP_SOURCE}")

‎dll/win32/shlwapi/policy.cpp‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <shlguid_undoc.h>
1414
#include <atlstr.h>
1515
#include <strsafe.h>
16+
#include <new>
1617
#include <wine/debug.h>
1718

1819
WINE_DEFAULT_DEBUG_CHANNEL(policy);
@@ -84,17 +85,6 @@ class CPolicyCache
8485
CloseHandle(m_hGlobalCounter);
8586
}
8687

87-
static void* operator new(size_t size)
88-
{
89-
// Returns NULL on failure; caller must check before use.
90-
// NOTE: C++ UB if constructor runs on NULL, but ReactOS convention.
91-
return LocalAlloc(LPTR, size);
92-
}
93-
static void operator delete(void *ptr)
94-
{
95-
LocalFree(ptr);
96-
}
97-
9888
BOOL Initialize(const SHPOLICY_ITEM *pItems, UINT cItems)
9989
{
10090
m_nCounterValue = MINLONG;
@@ -257,7 +247,7 @@ static BOOL SHPolicyCache_Create(VOID)
257247
if (g_pPolicyCache)
258248
return TRUE;
259249

260-
CPolicyCache *pCache = new CPolicyCache;
250+
CPolicyCache* pCache = new(std::nothrow) CPolicyCache;
261251
if (!pCache)
262252
return FALSE;
263253

‎dll/win32/shlwapi/propbag.cpp‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <atlcomcli.h> // for CComVariant
1717
#include <atlconv.h> // for CA2W and CW2A
1818
#include <strsafe.h> // for StringC... functions
19+
#include <cstdlib> // __min
20+
#include <new> // std::nothrow
1921

2022
WINE_DEFAULT_DEBUG_CHANNEL(shell);
2123

@@ -257,7 +259,9 @@ SHCreatePropertyBagOnMemory(_In_ DWORD dwMode, _In_ REFIID riid, _Out_ void **pp
257259

258260
*ppvObj = NULL;
259261

260-
CComPtr<CMemPropertyBag> pMemBag(new CMemPropertyBag(dwMode));
262+
CComPtr<CMemPropertyBag> pMemBag(new(std::nothrow) CMemPropertyBag(dwMode));
263+
if (!pMemBag)
264+
return E_OUTOFMEMORY;
261265

262266
return pMemBag->QueryInterface(riid, ppvObj);
263267
}
@@ -584,7 +588,9 @@ SHCreatePropertyBagOnRegKey(
584588

585589
*ppvObj = NULL;
586590

587-
CComPtr<CRegPropertyBag> pRegBag(new CRegPropertyBag(dwMode));
591+
CComPtr<CRegPropertyBag> pRegBag(new(std::nothrow) CRegPropertyBag(dwMode));
592+
if (!pRegBag)
593+
return E_OUTOFMEMORY;
588594

589595
HRESULT hr = pRegBag->Init(hKey, pszSubKey);
590596
if (FAILED(hr))
@@ -836,7 +842,7 @@ CIniPropertyBag::_GetSectionAndName(
836842
if (pchSep)
837843
{
838844
UINT cchSep = (UINT)(pchSep - pszStart + 1);
839-
StrCpyNW(pszSection, pszStart, min(cchSep, cchSectionMax));
845+
StrCpyNW(pszSection, pszStart, __min(cchSep, cchSectionMax));
840846
StrCpyNW(pszName, pchSep + 1, cchNameMax);
841847
return S_OK;
842848
}
@@ -995,7 +1001,9 @@ SHCreatePropertyBagOnProfileSection(
9951001
if (!PathFileExistsW(lpFileName))
9961002
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
9971003

998-
CComPtr<CIniPropertyBag> pIniPB(new CIniPropertyBag(dwMode));
1004+
CComPtr<CIniPropertyBag> pIniPB(new(std::nothrow) CIniPropertyBag(dwMode));
1005+
if (!pIniPB)
1006+
return E_OUTOFMEMORY;
9991007

10001008
HRESULT hr = pIniPB->Init(lpFileName, pszSection);
10011009
if (FAILED(hr))
@@ -1197,7 +1205,9 @@ CDesktopUpgradePropertyBag::Read(
11971205
HRESULT SHGetDesktopUpgradePropertyBag(REFIID riid, void **ppvObj)
11981206
{
11991207
*ppvObj = NULL;
1200-
CComPtr<CDesktopUpgradePropertyBag> pPropBag(new CDesktopUpgradePropertyBag());
1208+
CComPtr<CDesktopUpgradePropertyBag> pPropBag(new(std::nothrow) CDesktopUpgradePropertyBag());
1209+
if (!pPropBag)
1210+
return E_OUTOFMEMORY;
12011211
return pPropBag->QueryInterface(riid, ppvObj);
12021212
}
12031213

@@ -1908,7 +1918,12 @@ SHGetViewStatePropertyBag(
19081918
return E_FAIL;
19091919
}
19101920

1911-
CComPtr<CViewStatePropertyBag> pBag(new CViewStatePropertyBag());
1921+
CComPtr<CViewStatePropertyBag> pBag(new(std::nothrow) CViewStatePropertyBag());
1922+
if (!pBag)
1923+
{
1924+
::LeaveCriticalSection(&g_csBagCacheLock);
1925+
return E_OUTOFMEMORY;
1926+
}
19121927

19131928
hr = pBag->Init(pidl, bag_name, flags);
19141929
if (FAILED(hr))

‎dll/win32/shlwapi/querysrc.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <shlwapi_undoc.h>
1111
#include <shlobj_undoc.h>
1212
#include <shlguid_undoc.h>
13+
#include <new>
1314

1415
WINE_DEFAULT_DEBUG_CHANNEL(shell);
1516

@@ -305,7 +306,9 @@ STDMETHODIMP_(ULONG) CRegistrySource::Release()
305306

306307
STDMETHODIMP CRegistrySource::EnumValues(IEnumString **ppEnum)
307308
{
308-
CRegistryEnumValues* pEnum = new CRegistryEnumValues();
309+
CRegistryEnumValues* pEnum = new(std::nothrow) CRegistryEnumValues();
310+
if (!pEnum)
311+
return E_OUTOFMEMORY;
309312
HRESULT hr = pEnum->Init(m_hKey, this);
310313
if (FAILED(hr))
311314
{
@@ -318,7 +321,9 @@ STDMETHODIMP CRegistrySource::EnumValues(IEnumString **ppEnum)
318321

319322
STDMETHODIMP CRegistrySource::EnumSources(IEnumString **ppEnum)
320323
{
321-
CRegistryEnumKeys* pEnum = new CRegistryEnumKeys();
324+
CRegistryEnumKeys* pEnum = new(std::nothrow) CRegistryEnumKeys();
325+
if (!pEnum)
326+
return E_OUTOFMEMORY;
322327
HRESULT hr = pEnum->Init(m_hKey, this);
323328
if (FAILED(hr))
324329
{
@@ -500,7 +505,9 @@ QuerySourceCreateFromKey(
500505
{
501506
*ppv = NULL;
502507

503-
CRegistrySource* pRS = new CRegistrySource();
508+
CRegistrySource* pRS = new(std::nothrow) CRegistrySource();
509+
if (!pRS)
510+
return E_OUTOFMEMORY;
504511

505512
HRESULT hr = pRS->Init(hKey, lpSubKey, bCreate);
506513
if (SUCCEEDED(hr))

0 commit comments

Comments
 (0)