summaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/memutil.cpp
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2022-05-26 17:32:45 -0500
committerSean Hall <r.sean.hall@gmail.com>2022-05-31 13:20:44 -0500
commit0f9931107ecf9e1f6714e6fd2cabc76d2ddb1153 (patch)
tree0b20cc3b0fd9f21616d09420fa0c1a90c20a70a0 /src/libs/dutil/WixToolset.DUtil/memutil.cpp
parent6f062d1e816ff8d3127ddcbdc7ee0335873bbab8 (diff)
downloadwix-0f9931107ecf9e1f6714e6fd2cabc76d2ddb1153.tar.gz
wix-0f9931107ecf9e1f6714e6fd2cabc76d2ddb1153.tar.bz2
wix-0f9931107ecf9e1f6714e6fd2cabc76d2ddb1153.zip
Add MemSizeChecked.
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/memutil.cpp')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/memutil.cpp51
1 files changed, 39 insertions, 12 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/memutil.cpp b/src/libs/dutil/WixToolset.DUtil/memutil.cpp
index 977c189e..2ec04e5e 100644
--- a/src/libs/dutil/WixToolset.DUtil/memutil.cpp
+++ b/src/libs/dutil/WixToolset.DUtil/memutil.cpp
@@ -9,6 +9,7 @@
9#define MemExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) 9#define MemExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__)
10#define MemExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) 10#define MemExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__)
11#define MemExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) 11#define MemExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__)
12#define MemExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_MEMUTIL, x, e, s, __VA_ARGS__)
12#define MemExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__) 13#define MemExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_MEMUTIL, x, s, __VA_ARGS__)
13#define MemExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_MEMUTIL, p, x, e, s, __VA_ARGS__) 14#define MemExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_MEMUTIL, p, x, e, s, __VA_ARGS__)
14#define MemExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, p, x, s, __VA_ARGS__) 15#define MemExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_MEMUTIL, p, x, s, __VA_ARGS__)
@@ -74,6 +75,7 @@ extern "C" HRESULT DAPI MemReAllocSecure(
74 HRESULT hr = S_OK; 75 HRESULT hr = S_OK;
75 DWORD dwFlags = HEAP_REALLOC_IN_PLACE_ONLY; 76 DWORD dwFlags = HEAP_REALLOC_IN_PLACE_ONLY;
76 LPVOID pvNew = NULL; 77 LPVOID pvNew = NULL;
78 SIZE_T cb = 0;
77 79
78 dwFlags |= fZero ? HEAP_ZERO_MEMORY : 0; 80 dwFlags |= fZero ? HEAP_ZERO_MEMORY : 0;
79 pvNew = ::HeapReAlloc(::GetProcessHeap(), dwFlags, pv, cbSize); 81 pvNew = ::HeapReAlloc(::GetProcessHeap(), dwFlags, pv, cbSize);
@@ -82,18 +84,16 @@ extern "C" HRESULT DAPI MemReAllocSecure(
82 pvNew = MemAlloc(cbSize, fZero); 84 pvNew = MemAlloc(cbSize, fZero);
83 if (pvNew) 85 if (pvNew)
84 { 86 {
85 const SIZE_T cbCurrent = MemSize(pv); 87 hr = MemSizeChecked(pv, &cb);
86 if (-1 == cbCurrent) 88 MemExitOnFailure(hr, "Failed to get current memory size.");
87 { 89
88 MemExitOnRootFailure(hr = E_INVALIDARG, "Failed to get memory size"); 90 const SIZE_T cbCurrent = cb;
89 }
90 91
91 // HeapReAlloc may allocate more memory than requested. 92 // HeapReAlloc may allocate more memory than requested.
92 const SIZE_T cbNew = MemSize(pvNew); 93 hr = MemSizeChecked(pvNew, &cb);
93 if (-1 == cbNew) 94 MemExitOnFailure(hr, "Failed to get new memory size.");
94 { 95
95 MemExitOnRootFailure(hr = E_INVALIDARG, "Failed to get memory size"); 96 const SIZE_T cbNew = cb;
96 }
97 97
98 cbSize = cbNew; 98 cbSize = cbNew;
99 if (cbSize > cbCurrent) 99 if (cbSize > cbCurrent)
@@ -149,7 +149,10 @@ extern "C" HRESULT DAPI MemReAllocArray(
149 149
150 if (*ppvArray) 150 if (*ppvArray)
151 { 151 {
152 SIZE_T cbCurrent = MemSize(*ppvArray); 152 SIZE_T cbCurrent = 0;
153 hr = MemSizeChecked(*ppvArray, &cbCurrent);
154 MemExitOnFailure(hr, "Failed to get current memory size.");
155
153 if (cbCurrent < cbNew) 156 if (cbCurrent < cbNew)
154 { 157 {
155 pvNew = MemReAlloc(*ppvArray, cbNew, TRUE); 158 pvNew = MemReAlloc(*ppvArray, cbNew, TRUE);
@@ -192,7 +195,11 @@ extern "C" HRESULT DAPI MemEnsureArraySize(
192 if (*ppvArray) 195 if (*ppvArray)
193 { 196 {
194 SIZE_T cbUsed = cArray * cbArrayType; 197 SIZE_T cbUsed = cArray * cbArrayType;
195 SIZE_T cbCurrent = MemSize(*ppvArray); 198 SIZE_T cbCurrent = 0;
199
200 hr = MemSizeChecked(*ppvArray, &cbCurrent);
201 MemExitOnFailure(hr, "Failed to get current memory size.");
202
196 if (cbCurrent < cbUsed) 203 if (cbCurrent < cbUsed)
197 { 204 {
198 pvNew = MemReAlloc(*ppvArray, cbNew, TRUE); 205 pvNew = MemReAlloc(*ppvArray, cbNew, TRUE);
@@ -355,3 +362,23 @@ extern "C" SIZE_T DAPI MemSize(
355// AssertSz(vfMemInitialized, "MemInitialize() not called, this would normally crash"); 362// AssertSz(vfMemInitialized, "MemInitialize() not called, this would normally crash");
356 return ::HeapSize(::GetProcessHeap(), 0, pv); 363 return ::HeapSize(::GetProcessHeap(), 0, pv);
357} 364}
365
366
367extern "C" HRESULT DAPI MemSizeChecked(
368 __in LPCVOID pv,
369 __out SIZE_T* pcb
370 )
371{
372 HRESULT hr = S_OK;
373
374// AssertSz(vfMemInitialized, "MemInitialize() not called, this would normally crash");
375 *pcb = MemSize(pv);
376
377 if (-1 == *pcb)
378 {
379 MemExitWithRootFailure(hr, E_INVALIDARG, "Failed to get memory size");
380 }
381
382LExit:
383 return hr;
384}