diff options
Diffstat (limited to '')
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/buffutil.cpp | 6 | ||||
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/inc/memutil.h | 4 | ||||
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/inc/strutil.h | 8 | ||||
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/memutil.cpp | 51 | ||||
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/metautil.cpp | 5 | ||||
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/strutil.cpp | 145 | ||||
| -rw-r--r-- | src/libs/dutil/WixToolset.DUtil/thmutil.cpp | 11 | ||||
| -rw-r--r-- | src/libs/dutil/test/DUtilUnitTest/MemUtilTest.cpp | 9 |
8 files changed, 130 insertions, 109 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/buffutil.cpp b/src/libs/dutil/WixToolset.DUtil/buffutil.cpp index b6d58cc0..acde4dc9 100644 --- a/src/libs/dutil/WixToolset.DUtil/buffutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/buffutil.cpp | |||
| @@ -508,10 +508,14 @@ static HRESULT EnsureBufferSize( | |||
| 508 | { | 508 | { |
| 509 | HRESULT hr = S_OK; | 509 | HRESULT hr = S_OK; |
| 510 | SIZE_T cbTarget = ((cbSize / BUFFER_INCREMENT) + 1) * BUFFER_INCREMENT; | 510 | SIZE_T cbTarget = ((cbSize / BUFFER_INCREMENT) + 1) * BUFFER_INCREMENT; |
| 511 | SIZE_T cbCurrent = 0; | ||
| 511 | 512 | ||
| 512 | if (*ppbBuffer) | 513 | if (*ppbBuffer) |
| 513 | { | 514 | { |
| 514 | if (MemSize(*ppbBuffer) < cbTarget) | 515 | hr = MemSizeChecked(*ppbBuffer, &cbCurrent); |
| 516 | BuffExitOnFailure(hr, "Failed to get current buffer size."); | ||
| 517 | |||
| 518 | if (cbCurrent < cbTarget) | ||
| 515 | { | 519 | { |
| 516 | LPVOID pv = MemReAlloc(*ppbBuffer, cbTarget, TRUE); | 520 | LPVOID pv = MemReAlloc(*ppbBuffer, cbTarget, TRUE); |
| 517 | BuffExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to reallocate buffer."); | 521 | BuffExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to reallocate buffer."); |
diff --git a/src/libs/dutil/WixToolset.DUtil/inc/memutil.h b/src/libs/dutil/WixToolset.DUtil/inc/memutil.h index b8269269..c4a3b7b8 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/memutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/memutil.h | |||
| @@ -80,6 +80,10 @@ HRESULT DAPI MemFree( | |||
| 80 | SIZE_T DAPI MemSize( | 80 | SIZE_T DAPI MemSize( |
| 81 | __in LPCVOID pv | 81 | __in LPCVOID pv |
| 82 | ); | 82 | ); |
| 83 | HRESULT DAPI MemSizeChecked( | ||
| 84 | __in LPCVOID pv, | ||
| 85 | __out SIZE_T* pcb | ||
| 86 | ); | ||
| 83 | 87 | ||
| 84 | #ifdef __cplusplus | 88 | #ifdef __cplusplus |
| 85 | } | 89 | } |
diff --git a/src/libs/dutil/WixToolset.DUtil/inc/strutil.h b/src/libs/dutil/WixToolset.DUtil/inc/strutil.h index 1cff9ab8..f2324a80 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/strutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/strutil.h | |||
| @@ -139,11 +139,15 @@ HRESULT DAPI StrAllocFromError( | |||
| 139 | 139 | ||
| 140 | HRESULT DAPI StrMaxLength( | 140 | HRESULT DAPI StrMaxLength( |
| 141 | __in LPCVOID p, | 141 | __in LPCVOID p, |
| 142 | __out SIZE_T* pcbch | 142 | __out SIZE_T* pcch |
| 143 | ); | ||
| 144 | HRESULT DAPI StrMaxLengthAnsi( | ||
| 145 | __in LPCVOID p, | ||
| 146 | __out SIZE_T* pcch | ||
| 143 | ); | 147 | ); |
| 144 | HRESULT DAPI StrSize( | 148 | HRESULT DAPI StrSize( |
| 145 | __in LPCVOID p, | 149 | __in LPCVOID p, |
| 146 | __out SIZE_T* pcbb | 150 | __out SIZE_T* pcb |
| 147 | ); | 151 | ); |
| 148 | 152 | ||
| 149 | HRESULT DAPI StrFree( | 153 | HRESULT DAPI StrFree( |
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 | |||
| 367 | extern "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 | |||
| 382 | LExit: | ||
| 383 | return hr; | ||
| 384 | } | ||
diff --git a/src/libs/dutil/WixToolset.DUtil/metautil.cpp b/src/libs/dutil/WixToolset.DUtil/metautil.cpp index f313fc1c..c36aa96c 100644 --- a/src/libs/dutil/WixToolset.DUtil/metautil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/metautil.cpp | |||
| @@ -300,7 +300,10 @@ extern "C" HRESULT DAPI MetaGetValue( | |||
| 300 | } | 300 | } |
| 301 | else // set the size of the data to the actual size of the memory | 301 | else // set the size of the data to the actual size of the memory |
| 302 | { | 302 | { |
| 303 | SIZE_T cb = MemSize(pmr->pbMDData); | 303 | SIZE_T cb = 0; |
| 304 | hr = MemSizeChecked(pmr->pbMDData, &cb); | ||
| 305 | MetaExitOnFailure(hr, "failed to get metabase size"); | ||
| 306 | |||
| 304 | if (cb > DWORD_MAX) | 307 | if (cb > DWORD_MAX) |
| 305 | { | 308 | { |
| 306 | MetaExitOnRootFailure(hr = E_INVALIDSTATE, "metabase data is too large: %Iu", cb); | 309 | MetaExitOnRootFailure(hr = E_INVALIDSTATE, "metabase data is too large: %Iu", cb); |
diff --git a/src/libs/dutil/WixToolset.DUtil/strutil.cpp b/src/libs/dutil/WixToolset.DUtil/strutil.cpp index e4fcc9c8..a483cf54 100644 --- a/src/libs/dutil/WixToolset.DUtil/strutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/strutil.cpp | |||
| @@ -390,13 +390,8 @@ static HRESULT AllocStringHelper( | |||
| 390 | 390 | ||
| 391 | if (*ppwz) | 391 | if (*ppwz) |
| 392 | { | 392 | { |
| 393 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 393 | hr = StrMaxLength(*ppwz, &cch); |
| 394 | if (-1 == cch) | 394 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 395 | { | ||
| 396 | hr = E_INVALIDARG; | ||
| 397 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
| 398 | } | ||
| 399 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
| 400 | } | 395 | } |
| 401 | 396 | ||
| 402 | if (0 == cchSource && wzSource) | 397 | if (0 == cchSource && wzSource) |
| @@ -447,13 +442,8 @@ extern "C" HRESULT DAPI StrAnsiAllocString( | |||
| 447 | 442 | ||
| 448 | if (*ppsz) | 443 | if (*ppsz) |
| 449 | { | 444 | { |
| 450 | cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) | 445 | hr = StrMaxLengthAnsi(*ppsz, &cch); |
| 451 | if (-1 == cch) | 446 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 452 | { | ||
| 453 | hr = E_INVALIDARG; | ||
| 454 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
| 455 | } | ||
| 456 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | ||
| 457 | } | 447 | } |
| 458 | 448 | ||
| 459 | if (0 == cchSource) | 449 | if (0 == cchSource) |
| @@ -527,13 +517,8 @@ extern "C" HRESULT DAPI StrAllocStringAnsi( | |||
| 527 | 517 | ||
| 528 | if (*ppwz) | 518 | if (*ppwz) |
| 529 | { | 519 | { |
| 530 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 520 | hr = StrMaxLength(*ppwz, &cch); |
| 531 | if (-1 == cch) | 521 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 532 | { | ||
| 533 | hr = E_INVALIDARG; | ||
| 534 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
| 535 | } | ||
| 536 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
| 537 | } | 522 | } |
| 538 | 523 | ||
| 539 | if (0 == cchSource) | 524 | if (0 == cchSource) |
| @@ -605,13 +590,8 @@ HRESULT DAPI StrAnsiAllocStringAnsi( | |||
| 605 | 590 | ||
| 606 | if (*ppsz) | 591 | if (*ppsz) |
| 607 | { | 592 | { |
| 608 | cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) | 593 | hr = StrMaxLengthAnsi(*ppsz, &cch); |
| 609 | if (-1 == cch) | 594 | StrExitOnRootFailure(hr, "failed to get size of destination string"); |
| 610 | { | ||
| 611 | hr = E_INVALIDARG; | ||
| 612 | StrExitOnRootFailure(hr, "failed to get size of destination string"); | ||
| 613 | } | ||
| 614 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | ||
| 615 | } | 595 | } |
| 616 | 596 | ||
| 617 | if (0 == cchSource && szSource) | 597 | if (0 == cchSource && szSource) |
| @@ -664,13 +644,8 @@ extern "C" HRESULT DAPI StrAllocPrefix( | |||
| 664 | 644 | ||
| 665 | if (*ppwz) | 645 | if (*ppwz) |
| 666 | { | 646 | { |
| 667 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 647 | hr = StrMaxLength(*ppwz, &cch); |
| 668 | if (-1 == cch) | 648 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 669 | { | ||
| 670 | hr = E_INVALIDARG; | ||
| 671 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
| 672 | } | ||
| 673 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
| 674 | 649 | ||
| 675 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 650 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
| 676 | StrExitOnFailure(hr, "Failed to calculate length of string"); | 651 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
| @@ -770,13 +745,8 @@ static HRESULT AllocConcatHelper( | |||
| 770 | 745 | ||
| 771 | if (*ppwz) | 746 | if (*ppwz) |
| 772 | { | 747 | { |
| 773 | cch = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 748 | hr = StrMaxLength(*ppwz, &cch); |
| 774 | if (-1 == cch) | 749 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 775 | { | ||
| 776 | hr = E_INVALIDARG; | ||
| 777 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
| 778 | } | ||
| 779 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | ||
| 780 | 750 | ||
| 781 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 751 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
| 782 | StrExitOnFailure(hr, "Failed to calculate length of string"); | 752 | StrExitOnFailure(hr, "Failed to calculate length of string"); |
| @@ -833,13 +803,8 @@ extern "C" HRESULT DAPI StrAnsiAllocConcat( | |||
| 833 | 803 | ||
| 834 | if (*ppz) | 804 | if (*ppz) |
| 835 | { | 805 | { |
| 836 | cch = MemSize(*ppz); // get the count in bytes so we can check if it failed (returns -1) | 806 | hr = StrMaxLengthAnsi(*ppz, &cch); |
| 837 | if (-1 == cch) | 807 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 838 | { | ||
| 839 | hr = E_INVALIDARG; | ||
| 840 | StrExitOnFailure(hr, "failed to get size of destination string"); | ||
| 841 | } | ||
| 842 | cch /= sizeof(CHAR); // convert the count in bytes to count in characters | ||
| 843 | 808 | ||
| 844 | #pragma prefast(push) | 809 | #pragma prefast(push) |
| 845 | #pragma prefast(disable:25068) | 810 | #pragma prefast(disable:25068) |
| @@ -1085,12 +1050,8 @@ static HRESULT AllocFormattedArgsHelper( | |||
| 1085 | 1050 | ||
| 1086 | if (*ppwz) | 1051 | if (*ppwz) |
| 1087 | { | 1052 | { |
| 1088 | cbOriginal = MemSize(*ppwz); // get the count in bytes so we can check if it failed (returns -1) | 1053 | hr = StrSize(*ppwz, &cbOriginal); |
| 1089 | if (-1 == cbOriginal) | 1054 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 1090 | { | ||
| 1091 | hr = E_INVALIDARG; | ||
| 1092 | StrExitOnRootFailure(hr, "failed to get size of destination string"); | ||
| 1093 | } | ||
| 1094 | 1055 | ||
| 1095 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters | 1056 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters |
| 1096 | 1057 | ||
| @@ -1161,19 +1122,14 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
| 1161 | Assert(ppsz && szFormat && *szFormat); | 1122 | Assert(ppsz && szFormat && *szFormat); |
| 1162 | 1123 | ||
| 1163 | HRESULT hr = S_OK; | 1124 | HRESULT hr = S_OK; |
| 1164 | SIZE_T cch = *ppsz ? MemSize(*ppsz) / sizeof(CHAR) : 0; | 1125 | SIZE_T cch = 0; |
| 1165 | LPSTR pszOriginal = NULL; | 1126 | LPSTR pszOriginal = NULL; |
| 1166 | size_t cchOriginal = 0; | 1127 | size_t cchOriginal = 0; |
| 1167 | 1128 | ||
| 1168 | if (*ppsz) | 1129 | if (*ppsz) |
| 1169 | { | 1130 | { |
| 1170 | cch = MemSize(*ppsz); // get the count in bytes so we can check if it failed (returns -1) | 1131 | hr = StrMaxLengthAnsi(*ppsz, &cch); |
| 1171 | if (-1 == cch) | 1132 | StrExitOnFailure(hr, "failed to get size of destination string"); |
| 1172 | { | ||
| 1173 | hr = E_INVALIDARG; | ||
| 1174 | StrExitOnRootFailure(hr, "failed to get size of destination string"); | ||
| 1175 | } | ||
| 1176 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | ||
| 1177 | 1133 | ||
| 1178 | hr = ::StringCchLengthA(*ppsz, STRSAFE_MAX_CCH, &cchOriginal); | 1134 | hr = ::StringCchLengthA(*ppsz, STRSAFE_MAX_CCH, &cchOriginal); |
| 1179 | StrExitOnRootFailure(hr, "failed to get length of original string"); | 1135 | StrExitOnRootFailure(hr, "failed to get length of original string"); |
| @@ -1280,11 +1236,8 @@ extern "C" HRESULT DAPI StrMaxLength( | |||
| 1280 | 1236 | ||
| 1281 | if (p) | 1237 | if (p) |
| 1282 | { | 1238 | { |
| 1283 | *pcch = MemSize(p); // get size of entire buffer | 1239 | hr = StrSize(p, pcch); |
| 1284 | if (-1 == *pcch) | 1240 | StrExitOnFailure(hr, "Failed to get size of string buffer."); |
| 1285 | { | ||
| 1286 | ExitFunction1(hr = E_FAIL); | ||
| 1287 | } | ||
| 1288 | 1241 | ||
| 1289 | *pcch /= sizeof(WCHAR); // reduce to count of characters | 1242 | *pcch /= sizeof(WCHAR); // reduce to count of characters |
| 1290 | } | 1243 | } |
| @@ -1300,27 +1253,51 @@ LExit: | |||
| 1300 | 1253 | ||
| 1301 | 1254 | ||
| 1302 | /******************************************************************** | 1255 | /******************************************************************** |
| 1303 | StrSize - returns count of bytes in dynamic string p | 1256 | StrMaxLengthAnsi - returns maximum number of characters that can be stored in dynamic string p |
| 1304 | 1257 | ||
| 1258 | NOTE: assumes non-Unicode string | ||
| 1305 | ********************************************************************/ | 1259 | ********************************************************************/ |
| 1306 | extern "C" HRESULT DAPI StrSize( | 1260 | extern "C" HRESULT DAPI StrMaxLengthAnsi( |
| 1307 | __in LPCVOID p, | 1261 | __in LPCVOID p, |
| 1308 | __out SIZE_T* pcb | 1262 | __out SIZE_T* pcch |
| 1309 | ) | 1263 | ) |
| 1310 | { | 1264 | { |
| 1311 | Assert(p && pcb); | 1265 | Assert(pcch); |
| 1312 | 1266 | ||
| 1313 | HRESULT hr = S_OK; | 1267 | HRESULT hr = S_OK; |
| 1314 | 1268 | ||
| 1315 | *pcb = MemSize(p); | 1269 | if (p) |
| 1316 | if (-1 == *pcb) | 1270 | { |
| 1271 | hr = StrSize(p, pcch); | ||
| 1272 | StrExitOnFailure(hr, "Failed to get size of string buffer."); | ||
| 1273 | |||
| 1274 | *pcch /= sizeof(CHAR); // reduce to count of characters | ||
| 1275 | } | ||
| 1276 | else | ||
| 1317 | { | 1277 | { |
| 1318 | hr = E_FAIL; | 1278 | *pcch = 0; |
| 1319 | } | 1279 | } |
| 1280 | Assert(S_OK == hr); | ||
| 1320 | 1281 | ||
| 1282 | LExit: | ||
| 1321 | return hr; | 1283 | return hr; |
| 1322 | } | 1284 | } |
| 1323 | 1285 | ||
| 1286 | |||
| 1287 | /******************************************************************** | ||
| 1288 | StrSize - returns count of bytes in dynamic string p | ||
| 1289 | |||
| 1290 | ********************************************************************/ | ||
| 1291 | extern "C" HRESULT DAPI StrSize( | ||
| 1292 | __in LPCVOID p, | ||
| 1293 | __out SIZE_T* pcb | ||
| 1294 | ) | ||
| 1295 | { | ||
| 1296 | Assert(p && pcb); | ||
| 1297 | |||
| 1298 | return MemSizeChecked(p, pcb); | ||
| 1299 | } | ||
| 1300 | |||
| 1324 | /******************************************************************** | 1301 | /******************************************************************** |
| 1325 | StrFree - releases dynamic string memory allocated by any StrAlloc*() functions | 1302 | StrFree - releases dynamic string memory allocated by any StrAlloc*() functions |
| 1326 | 1303 | ||
| @@ -2786,22 +2763,16 @@ extern "C" DAPI_(HRESULT) StrSecureZeroString( | |||
| 2786 | ) | 2763 | ) |
| 2787 | { | 2764 | { |
| 2788 | HRESULT hr = S_OK; | 2765 | HRESULT hr = S_OK; |
| 2789 | SIZE_T cch; | 2766 | SIZE_T cb = 0; |
| 2790 | 2767 | ||
| 2791 | if (pwz) | 2768 | if (pwz) |
| 2792 | { | 2769 | { |
| 2793 | cch = MemSize(pwz); | 2770 | hr = StrSize(pwz, &cb); |
| 2794 | if (-1 == cch) | 2771 | StrExitOnFailure(hr, "Failed to get size of string"); |
| 2795 | { | 2772 | |
| 2796 | hr = E_INVALIDARG; | 2773 | SecureZeroMemory(pwz, cb); |
| 2797 | StrExitOnFailure(hr, "Failed to get size of string"); | ||
| 2798 | } | ||
| 2799 | else | ||
| 2800 | { | ||
| 2801 | SecureZeroMemory(pwz, cch); | ||
| 2802 | } | ||
| 2803 | } | 2774 | } |
| 2804 | 2775 | ||
| 2805 | LExit: | 2776 | LExit: |
| 2806 | return hr; | 2777 | return hr; |
| 2807 | } | 2778 | } |
diff --git a/src/libs/dutil/WixToolset.DUtil/thmutil.cpp b/src/libs/dutil/WixToolset.DUtil/thmutil.cpp index d3d32176..52a52a1e 100644 --- a/src/libs/dutil/WixToolset.DUtil/thmutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/thmutil.cpp | |||
| @@ -1189,6 +1189,7 @@ DAPI_(HRESULT) ThemeShowPageEx( | |||
| 1189 | BOOL fSaveEditboxes = FALSE; | 1189 | BOOL fSaveEditboxes = FALSE; |
| 1190 | THEME_SAVEDVARIABLE* pSavedVariable = NULL; | 1190 | THEME_SAVEDVARIABLE* pSavedVariable = NULL; |
| 1191 | THEME_PAGE* pPage = ThemeGetPage(pTheme, dwPage); | 1191 | THEME_PAGE* pPage = ThemeGetPage(pTheme, dwPage); |
| 1192 | SIZE_T cb = 0; | ||
| 1192 | 1193 | ||
| 1193 | if (pPage) | 1194 | if (pPage) |
| 1194 | { | 1195 | { |
| @@ -1219,9 +1220,9 @@ DAPI_(HRESULT) ThemeShowPageEx( | |||
| 1219 | if (THEME_SHOW_PAGE_REASON_REFRESH != reason) | 1220 | if (THEME_SHOW_PAGE_REASON_REFRESH != reason) |
| 1220 | { | 1221 | { |
| 1221 | pPage->cSavedVariables = 0; | 1222 | pPage->cSavedVariables = 0; |
| 1222 | if (pPage->rgSavedVariables) | 1223 | if (pPage->rgSavedVariables && SUCCEEDED(MemSizeChecked(pPage->rgSavedVariables, &cb))) |
| 1223 | { | 1224 | { |
| 1224 | SecureZeroMemory(pPage->rgSavedVariables, MemSize(pPage->rgSavedVariables)); | 1225 | SecureZeroMemory(pPage->rgSavedVariables, cb); |
| 1225 | } | 1226 | } |
| 1226 | } | 1227 | } |
| 1227 | 1228 | ||
| @@ -1238,7 +1239,11 @@ DAPI_(HRESULT) ThemeShowPageEx( | |||
| 1238 | hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&pPage->rgSavedVariables), pPage->cControlIndices, sizeof(THEME_SAVEDVARIABLE), pPage->cControlIndices); | 1239 | hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&pPage->rgSavedVariables), pPage->cControlIndices, sizeof(THEME_SAVEDVARIABLE), pPage->cControlIndices); |
| 1239 | ThmExitOnFailure(hr, "Failed to allocate memory for saved variables."); | 1240 | ThmExitOnFailure(hr, "Failed to allocate memory for saved variables."); |
| 1240 | 1241 | ||
| 1241 | SecureZeroMemory(pPage->rgSavedVariables, MemSize(pPage->rgSavedVariables)); | 1242 | if (SUCCEEDED(MemSizeChecked(pPage->rgSavedVariables, &cb))) |
| 1243 | { | ||
| 1244 | SecureZeroMemory(pPage->rgSavedVariables, cb); | ||
| 1245 | } | ||
| 1246 | |||
| 1242 | pPage->cSavedVariables = pPage->cControlIndices; | 1247 | pPage->cSavedVariables = pPage->cControlIndices; |
| 1243 | 1248 | ||
| 1244 | // Save the variables in the loop below. | 1249 | // Save the variables in the loop below. |
diff --git a/src/libs/dutil/test/DUtilUnitTest/MemUtilTest.cpp b/src/libs/dutil/test/DUtilUnitTest/MemUtilTest.cpp index 09692bfb..520ed426 100644 --- a/src/libs/dutil/test/DUtilUnitTest/MemUtilTest.cpp +++ b/src/libs/dutil/test/DUtilUnitTest/MemUtilTest.cpp | |||
| @@ -23,7 +23,7 @@ namespace DutilTests | |||
| 23 | void MemUtilAppendTest() | 23 | void MemUtilAppendTest() |
| 24 | { | 24 | { |
| 25 | HRESULT hr = S_OK; | 25 | HRESULT hr = S_OK; |
| 26 | DWORD dwSize; | 26 | SIZE_T cbSize = 0; |
| 27 | ArrayValue *rgValues = NULL; | 27 | ArrayValue *rgValues = NULL; |
| 28 | DWORD cValues = 0; | 28 | DWORD cValues = 0; |
| 29 | 29 | ||
| @@ -65,8 +65,11 @@ namespace DutilTests | |||
| 65 | // and make sure it doesn't grow since we already have enough space | 65 | // and make sure it doesn't grow since we already have enough space |
| 66 | hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&rgValues), cValues, sizeof(ArrayValue), 5); | 66 | hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&rgValues), cValues, sizeof(ArrayValue), 5); |
| 67 | NativeAssert::Succeeded(hr, "Failed to ensure array size matches what it should already be"); | 67 | NativeAssert::Succeeded(hr, "Failed to ensure array size matches what it should already be"); |
| 68 | dwSize = MemSize(rgValues); | 68 | |
| 69 | if (dwSize != 6 * sizeof(ArrayValue)) | 69 | hr = MemSizeChecked(rgValues, &cbSize); |
| 70 | NativeAssert::Succeeded(hr, "Failed to get current array size"); | ||
| 71 | |||
| 72 | if (cbSize != 6 * sizeof(ArrayValue)) | ||
| 70 | { | 73 | { |
| 71 | hr = E_FAIL; | 74 | hr = E_FAIL; |
| 72 | ExitOnFailure(hr, "MemEnsureArraySize is growing an array that is already big enough!"); | 75 | ExitOnFailure(hr, "MemEnsureArraySize is growing an array that is already big enough!"); |
