diff options
| author | Sean Hall <r.sean.hall@gmail.com> | 2021-04-28 16:36:56 -0500 |
|---|---|---|
| committer | Sean Hall <r.sean.hall@gmail.com> | 2021-04-29 13:58:14 -0500 |
| commit | bcd3ee7ab858d62beb36af9f5986544b68a3dd35 (patch) | |
| tree | 424c4e61a580b7c4b7481712f69ab0193d76b9c4 /src/dutil/strutil.cpp | |
| parent | d73c29407fe5ec6a0207af7d9c2547457ae0854c (diff) | |
| download | wix-bcd3ee7ab858d62beb36af9f5986544b68a3dd35.tar.gz wix-bcd3ee7ab858d62beb36af9f5986544b68a3dd35.tar.bz2 wix-bcd3ee7ab858d62beb36af9f5986544b68a3dd35.zip | |
Clean up more 32-bit assumptions.
Diffstat (limited to 'src/dutil/strutil.cpp')
| -rw-r--r-- | src/dutil/strutil.cpp | 129 |
1 files changed, 87 insertions, 42 deletions
diff --git a/src/dutil/strutil.cpp b/src/dutil/strutil.cpp index daa090c9..550d6169 100644 --- a/src/dutil/strutil.cpp +++ b/src/dutil/strutil.cpp | |||
| @@ -46,7 +46,7 @@ static HRESULT AllocFormattedArgsHelper( | |||
| 46 | static HRESULT StrAllocStringMapInvariant( | 46 | static HRESULT StrAllocStringMapInvariant( |
| 47 | __deref_out_z LPWSTR* pscz, | 47 | __deref_out_z LPWSTR* pscz, |
| 48 | __in_z LPCWSTR wzSource, | 48 | __in_z LPCWSTR wzSource, |
| 49 | __in int cchSource, | 49 | __in SIZE_T cchSource, |
| 50 | __in DWORD dwMapFlags | 50 | __in DWORD dwMapFlags |
| 51 | ); | 51 | ); |
| 52 | 52 | ||
| @@ -146,7 +146,7 @@ HRESULT DAPI StrTrimCapacity( | |||
| 146 | SIZE_T cchLen = 0; | 146 | SIZE_T cchLen = 0; |
| 147 | 147 | ||
| 148 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); | 148 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, reinterpret_cast<UINT_PTR*>(&cchLen)); |
| 149 | StrExitOnFailure(hr, "Failed to calculate length of string"); | 149 | StrExitOnRootFailure(hr, "Failed to calculate length of string"); |
| 150 | 150 | ||
| 151 | ++cchLen; // Add 1 for null-terminator | 151 | ++cchLen; // Add 1 for null-terminator |
| 152 | 152 | ||
| @@ -170,7 +170,7 @@ HRESULT DAPI StrTrimWhitespace( | |||
| 170 | ) | 170 | ) |
| 171 | { | 171 | { |
| 172 | HRESULT hr = S_OK; | 172 | HRESULT hr = S_OK; |
| 173 | int i = 0; | 173 | size_t i = 0; |
| 174 | LPWSTR sczResult = NULL; | 174 | LPWSTR sczResult = NULL; |
| 175 | 175 | ||
| 176 | // Ignore beginning whitespace | 176 | // Ignore beginning whitespace |
| @@ -179,7 +179,9 @@ HRESULT DAPI StrTrimWhitespace( | |||
| 179 | wzSource++; | 179 | wzSource++; |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | i = lstrlenW(wzSource); | 182 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, &i); |
| 183 | StrExitOnRootFailure(hr, "Failed to get length of string"); | ||
| 184 | |||
| 183 | // Overwrite ending whitespace with null characters | 185 | // Overwrite ending whitespace with null characters |
| 184 | if (0 < i) | 186 | if (0 < i) |
| 185 | { | 187 | { |
| @@ -291,7 +293,7 @@ HRESULT DAPI StrAnsiTrimWhitespace( | |||
| 291 | ) | 293 | ) |
| 292 | { | 294 | { |
| 293 | HRESULT hr = S_OK; | 295 | HRESULT hr = S_OK; |
| 294 | int i = 0; | 296 | size_t i = 0; |
| 295 | LPSTR sczResult = NULL; | 297 | LPSTR sczResult = NULL; |
| 296 | 298 | ||
| 297 | // Ignore beginning whitespace | 299 | // Ignore beginning whitespace |
| @@ -300,7 +302,9 @@ HRESULT DAPI StrAnsiTrimWhitespace( | |||
| 300 | szSource++; | 302 | szSource++; |
| 301 | } | 303 | } |
| 302 | 304 | ||
| 303 | i = lstrlen(szSource); | 305 | hr = ::StringCchLengthA(szSource, STRSAFE_MAX_CCH, &i); |
| 306 | StrExitOnRootFailure(hr, "Failed to get length of string"); | ||
| 307 | |||
| 304 | // Overwrite ending whitespace with null characters | 308 | // Overwrite ending whitespace with null characters |
| 305 | if (0 < i) | 309 | if (0 < i) |
| 306 | { | 310 | { |
| @@ -395,14 +399,15 @@ static HRESULT AllocStringHelper( | |||
| 395 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters | 399 | cch /= sizeof(WCHAR); //convert the count in bytes to count in characters |
| 396 | } | 400 | } |
| 397 | 401 | ||
| 398 | if (0 == cchSource) | 402 | if (0 == cchSource && wzSource) |
| 399 | { | 403 | { |
| 400 | cchSource = lstrlenW(wzSource); | 404 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, reinterpret_cast<size_t*>(&cchSource)); |
| 405 | StrExitOnRootFailure(hr, "failed to get length of source string"); | ||
| 401 | } | 406 | } |
| 402 | 407 | ||
| 403 | SIZE_T cchNeeded; | 408 | SIZE_T cchNeeded; |
| 404 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator | 409 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator |
| 405 | StrExitOnFailure(hr, "source string is too long"); | 410 | StrExitOnRootFailure(hr, "source string is too long"); |
| 406 | 411 | ||
| 407 | if (cch < cchNeeded) | 412 | if (cch < cchNeeded) |
| 408 | { | 413 | { |
| @@ -604,19 +609,20 @@ HRESULT DAPI StrAnsiAllocStringAnsi( | |||
| 604 | if (-1 == cch) | 609 | if (-1 == cch) |
| 605 | { | 610 | { |
| 606 | hr = E_INVALIDARG; | 611 | hr = E_INVALIDARG; |
| 607 | StrExitOnFailure(hr, "failed to get size of destination string"); | 612 | StrExitOnRootFailure(hr, "failed to get size of destination string"); |
| 608 | } | 613 | } |
| 609 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | 614 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters |
| 610 | } | 615 | } |
| 611 | 616 | ||
| 612 | if (0 == cchSource) | 617 | if (0 == cchSource && szSource) |
| 613 | { | 618 | { |
| 614 | cchSource = lstrlenA(szSource); | 619 | hr = ::StringCchLengthA(szSource, STRSAFE_MAX_CCH, reinterpret_cast<size_t*>(&cchSource)); |
| 620 | StrExitOnRootFailure(hr, "failed to get length of source string"); | ||
| 615 | } | 621 | } |
| 616 | 622 | ||
| 617 | SIZE_T cchNeeded; | 623 | SIZE_T cchNeeded; |
| 618 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator | 624 | hr = ::ULongPtrAdd(cchSource, 1, &cchNeeded); // add one for the null terminator |
| 619 | StrExitOnFailure(hr, "source string is too long"); | 625 | StrExitOnRootFailure(hr, "source string is too long"); |
| 620 | 626 | ||
| 621 | if (cch < cchNeeded) | 627 | if (cch < cchNeeded) |
| 622 | { | 628 | { |
| @@ -1075,7 +1081,7 @@ static HRESULT AllocFormattedArgsHelper( | |||
| 1075 | SIZE_T cch = 0; | 1081 | SIZE_T cch = 0; |
| 1076 | LPWSTR pwzOriginal = NULL; | 1082 | LPWSTR pwzOriginal = NULL; |
| 1077 | SIZE_T cbOriginal = 0; | 1083 | SIZE_T cbOriginal = 0; |
| 1078 | SIZE_T cchOriginal = 0; | 1084 | size_t cchOriginal = 0; |
| 1079 | 1085 | ||
| 1080 | if (*ppwz) | 1086 | if (*ppwz) |
| 1081 | { | 1087 | { |
| @@ -1083,11 +1089,13 @@ static HRESULT AllocFormattedArgsHelper( | |||
| 1083 | if (-1 == cbOriginal) | 1089 | if (-1 == cbOriginal) |
| 1084 | { | 1090 | { |
| 1085 | hr = E_INVALIDARG; | 1091 | hr = E_INVALIDARG; |
| 1086 | StrExitOnFailure(hr, "failed to get size of destination string"); | 1092 | StrExitOnRootFailure(hr, "failed to get size of destination string"); |
| 1087 | } | 1093 | } |
| 1088 | 1094 | ||
| 1089 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters | 1095 | cch = cbOriginal / sizeof(WCHAR); //convert the count in bytes to count in characters |
| 1090 | cchOriginal = lstrlenW(*ppwz); | 1096 | |
| 1097 | hr = ::StringCchLengthW(*ppwz, STRSAFE_MAX_CCH, &cchOriginal); | ||
| 1098 | StrExitOnRootFailure(hr, "failed to get length of original string"); | ||
| 1091 | } | 1099 | } |
| 1092 | 1100 | ||
| 1093 | if (0 == cch) // if there is no space in the string buffer | 1101 | if (0 == cch) // if there is no space in the string buffer |
| @@ -1124,7 +1132,7 @@ static HRESULT AllocFormattedArgsHelper( | |||
| 1124 | hr = S_FALSE; | 1132 | hr = S_FALSE; |
| 1125 | } | 1133 | } |
| 1126 | } while (S_FALSE == hr); | 1134 | } while (S_FALSE == hr); |
| 1127 | StrExitOnFailure(hr, "failed to format string"); | 1135 | StrExitOnRootFailure(hr, "failed to format string"); |
| 1128 | 1136 | ||
| 1129 | LExit: | 1137 | LExit: |
| 1130 | if (pwzOriginal && fZeroOnRealloc) | 1138 | if (pwzOriginal && fZeroOnRealloc) |
| @@ -1155,7 +1163,7 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
| 1155 | HRESULT hr = S_OK; | 1163 | HRESULT hr = S_OK; |
| 1156 | SIZE_T cch = *ppsz ? MemSize(*ppsz) / sizeof(CHAR) : 0; | 1164 | SIZE_T cch = *ppsz ? MemSize(*ppsz) / sizeof(CHAR) : 0; |
| 1157 | LPSTR pszOriginal = NULL; | 1165 | LPSTR pszOriginal = NULL; |
| 1158 | DWORD cchOriginal = 0; | 1166 | size_t cchOriginal = 0; |
| 1159 | 1167 | ||
| 1160 | if (*ppsz) | 1168 | if (*ppsz) |
| 1161 | { | 1169 | { |
| @@ -1163,11 +1171,12 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
| 1163 | if (-1 == cch) | 1171 | if (-1 == cch) |
| 1164 | { | 1172 | { |
| 1165 | hr = E_INVALIDARG; | 1173 | hr = E_INVALIDARG; |
| 1166 | StrExitOnFailure(hr, "failed to get size of destination string"); | 1174 | StrExitOnRootFailure(hr, "failed to get size of destination string"); |
| 1167 | } | 1175 | } |
| 1168 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters | 1176 | cch /= sizeof(CHAR); //convert the count in bytes to count in characters |
| 1169 | 1177 | ||
| 1170 | cchOriginal = lstrlenA(*ppsz); | 1178 | hr = ::StringCchLengthA(*ppsz, STRSAFE_MAX_CCH, &cchOriginal); |
| 1179 | StrExitOnRootFailure(hr, "failed to get length of original string"); | ||
| 1171 | } | 1180 | } |
| 1172 | 1181 | ||
| 1173 | if (0 == cch) // if there is no space in the string buffer | 1182 | if (0 == cch) // if there is no space in the string buffer |
| @@ -1202,7 +1211,7 @@ extern "C" HRESULT DAPI StrAnsiAllocFormattedArgs( | |||
| 1202 | hr = S_FALSE; | 1211 | hr = S_FALSE; |
| 1203 | } | 1212 | } |
| 1204 | } while (S_FALSE == hr); | 1213 | } while (S_FALSE == hr); |
| 1205 | StrExitOnFailure(hr, "failed to format string"); | 1214 | StrExitOnRootFailure(hr, "failed to format string"); |
| 1206 | 1215 | ||
| 1207 | LExit: | 1216 | LExit: |
| 1208 | ReleaseStr(pszOriginal); | 1217 | ReleaseStr(pszOriginal); |
| @@ -1375,6 +1384,8 @@ extern "C" HRESULT DAPI StrReplaceString( | |||
| 1375 | HRESULT hr = S_FALSE; | 1384 | HRESULT hr = S_FALSE; |
| 1376 | LPCWSTR wzSubLocation = NULL; | 1385 | LPCWSTR wzSubLocation = NULL; |
| 1377 | LPWSTR pwzBuffer = NULL; | 1386 | LPWSTR pwzBuffer = NULL; |
| 1387 | size_t cchOldSubString = 0; | ||
| 1388 | size_t cchNewSubString = 0; | ||
| 1378 | 1389 | ||
| 1379 | if (!*ppwzOriginal) | 1390 | if (!*ppwzOriginal) |
| 1380 | { | 1391 | { |
| @@ -1387,8 +1398,20 @@ extern "C" HRESULT DAPI StrReplaceString( | |||
| 1387 | ExitFunction(); | 1398 | ExitFunction(); |
| 1388 | } | 1399 | } |
| 1389 | 1400 | ||
| 1401 | if (wzOldSubString) | ||
| 1402 | { | ||
| 1403 | hr = ::StringCchLengthW(wzOldSubString, STRSAFE_MAX_CCH, &cchOldSubString); | ||
| 1404 | StrExitOnRootFailure(hr, "Failed to get old string length."); | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | if (wzNewSubString) | ||
| 1408 | { | ||
| 1409 | hr = ::StringCchLengthW(wzNewSubString, STRSAFE_MAX_CCH, &cchNewSubString); | ||
| 1410 | StrExitOnRootFailure(hr, "Failed to get new string length."); | ||
| 1411 | } | ||
| 1412 | |||
| 1390 | hr = ::PtrdiffTToDWord(wzSubLocation - *ppwzOriginal, pdwStartIndex); | 1413 | hr = ::PtrdiffTToDWord(wzSubLocation - *ppwzOriginal, pdwStartIndex); |
| 1391 | StrExitOnFailure(hr, "Failed to diff pointers."); | 1414 | StrExitOnRootFailure(hr, "Failed to diff pointers."); |
| 1392 | 1415 | ||
| 1393 | hr = StrAllocString(&pwzBuffer, *ppwzOriginal, wzSubLocation - *ppwzOriginal); | 1416 | hr = StrAllocString(&pwzBuffer, *ppwzOriginal, wzSubLocation - *ppwzOriginal); |
| 1394 | StrExitOnFailure(hr, "Failed to duplicate string."); | 1417 | StrExitOnFailure(hr, "Failed to duplicate string."); |
| @@ -1398,14 +1421,14 @@ extern "C" HRESULT DAPI StrReplaceString( | |||
| 1398 | hr = StrAllocConcat(&pwzBuffer, wzNewSubString, 0); | 1421 | hr = StrAllocConcat(&pwzBuffer, wzNewSubString, 0); |
| 1399 | StrExitOnFailure(hr, "Failed to append new string."); | 1422 | StrExitOnFailure(hr, "Failed to append new string."); |
| 1400 | 1423 | ||
| 1401 | hr = StrAllocConcat(&pwzBuffer, wzSubLocation + wcslen(wzOldSubString), 0); | 1424 | hr = StrAllocConcat(&pwzBuffer, wzSubLocation + cchOldSubString, 0); |
| 1402 | StrExitOnFailure(hr, "Failed to append post string."); | 1425 | StrExitOnFailure(hr, "Failed to append post string."); |
| 1403 | 1426 | ||
| 1404 | hr = StrFree(*ppwzOriginal); | 1427 | hr = StrFree(*ppwzOriginal); |
| 1405 | StrExitOnFailure(hr, "Failed to free original string."); | 1428 | StrExitOnFailure(hr, "Failed to free original string."); |
| 1406 | 1429 | ||
| 1407 | *ppwzOriginal = pwzBuffer; | 1430 | *ppwzOriginal = pwzBuffer; |
| 1408 | *pdwStartIndex = *pdwStartIndex + static_cast<DWORD>(wcslen(wzNewSubString)); | 1431 | *pdwStartIndex = *pdwStartIndex + static_cast<DWORD>(cchNewSubString); |
| 1409 | hr = S_OK; | 1432 | hr = S_OK; |
| 1410 | 1433 | ||
| 1411 | LExit: | 1434 | LExit: |
| @@ -1516,15 +1539,18 @@ extern "C" HRESULT DAPI StrHexDecode( | |||
| 1516 | Assert(wzSource && pbDest); | 1539 | Assert(wzSource && pbDest); |
| 1517 | 1540 | ||
| 1518 | HRESULT hr = S_OK; | 1541 | HRESULT hr = S_OK; |
| 1519 | DWORD cchSource = lstrlenW(wzSource); | 1542 | size_t cchSource = 0; |
| 1520 | DWORD i; | 1543 | size_t i = 0; |
| 1521 | BYTE b; | 1544 | BYTE b = 0; |
| 1545 | |||
| 1546 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, &cchSource); | ||
| 1547 | StrExitOnRootFailure(hr, "Failed to get length of hex string: %ls", wzSource); | ||
| 1522 | 1548 | ||
| 1523 | Assert(0 == cchSource % 2); | 1549 | Assert(0 == cchSource % 2); |
| 1524 | if (cbDest < cchSource / 2) | 1550 | if (cbDest < cchSource / 2) |
| 1525 | { | 1551 | { |
| 1526 | hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); | 1552 | hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); |
| 1527 | StrExitOnRootFailure(hr, "Insufficient buffer to decode string '%ls' len: %u into %u bytes.", wzSource, cchSource, cbDest); | 1553 | StrExitOnRootFailure(hr, "Insufficient buffer to decode string '%ls' len: %Iu into %Iu bytes.", wzSource, cchSource, cbDest); |
| 1528 | } | 1554 | } |
| 1529 | 1555 | ||
| 1530 | for (i = 0; i < cchSource / 2; ++i) | 1556 | for (i = 0; i < cchSource / 2; ++i) |
| @@ -1728,17 +1754,20 @@ extern "C" HRESULT DAPI StrAllocBase85Decode( | |||
| 1728 | ) | 1754 | ) |
| 1729 | { | 1755 | { |
| 1730 | HRESULT hr = S_OK; | 1756 | HRESULT hr = S_OK; |
| 1731 | SIZE_T cchSource = lstrlenW(wzSource); | 1757 | size_t cchSource = 0; |
| 1732 | DWORD_PTR i, n, k; | 1758 | DWORD_PTR i, n, k; |
| 1733 | 1759 | ||
| 1734 | BYTE* pbDest; | 1760 | BYTE* pbDest = 0; |
| 1735 | SIZE_T cbDest; | 1761 | SIZE_T cbDest = 0; |
| 1736 | 1762 | ||
| 1737 | if (!wzSource || !ppbDest || !pcbDest) | 1763 | if (!wzSource || !ppbDest || !pcbDest) |
| 1738 | { | 1764 | { |
| 1739 | return E_INVALIDARG; | 1765 | ExitFunction1(hr = E_INVALIDARG); |
| 1740 | } | 1766 | } |
| 1741 | 1767 | ||
| 1768 | hr = ::StringCchLengthW(wzSource, STRSAFE_MAX_CCH, &cchSource); | ||
| 1769 | StrExitOnRootFailure(hr, "failed to get length of base 85 string: %ls", wzSource); | ||
| 1770 | |||
| 1742 | // evaluate size of output and check it | 1771 | // evaluate size of output and check it |
| 1743 | k = cchSource / 5; | 1772 | k = cchSource / 5; |
| 1744 | cbDest = k << 2; | 1773 | cbDest = k << 2; |
| @@ -1932,7 +1961,8 @@ extern "C" HRESULT DAPI MultiSzPrepend( | |||
| 1932 | StrExitOnFailure(hr, "failed to get length of multisz"); | 1961 | StrExitOnFailure(hr, "failed to get length of multisz"); |
| 1933 | } | 1962 | } |
| 1934 | 1963 | ||
| 1935 | cchInsert = lstrlenW(pwzInsert); | 1964 | hr = ::StringCchLengthW(pwzInsert, STRSAFE_MAX_CCH, reinterpret_cast<size_t*>(&cchInsert)); |
| 1965 | StrExitOnRootFailure(hr, "failed to get length of insert string"); | ||
| 1936 | 1966 | ||
| 1937 | cchResult = cchInsert + cchMultiSz + 1; | 1967 | cchResult = cchInsert + cchMultiSz + 1; |
| 1938 | 1968 | ||
| @@ -1942,7 +1972,7 @@ extern "C" HRESULT DAPI MultiSzPrepend( | |||
| 1942 | 1972 | ||
| 1943 | // Prepend | 1973 | // Prepend |
| 1944 | hr = ::StringCchCopyW(pwzResult, cchResult, pwzInsert); | 1974 | hr = ::StringCchCopyW(pwzResult, cchResult, pwzInsert); |
| 1945 | StrExitOnFailure(hr, "failed to copy prepend string: %ls", pwzInsert); | 1975 | StrExitOnRootFailure(hr, "failed to copy prepend string: %ls", pwzInsert); |
| 1946 | 1976 | ||
| 1947 | // If there was no MULTISZ, double null terminate our result, otherwise, copy the MULTISZ in | 1977 | // If there was no MULTISZ, double null terminate our result, otherwise, copy the MULTISZ in |
| 1948 | if (0 == cchMultiSz) | 1978 | if (0 == cchMultiSz) |
| @@ -2207,9 +2237,12 @@ extern "C" HRESULT DAPI MultiSzInsertString( | |||
| 2207 | SIZE_T cchProgress = 0; | 2237 | SIZE_T cchProgress = 0; |
| 2208 | LPWSTR pwzResult = NULL; | 2238 | LPWSTR pwzResult = NULL; |
| 2209 | SIZE_T cchResult = 0; | 2239 | SIZE_T cchResult = 0; |
| 2210 | SIZE_T cchString = lstrlenW(pwzInsert); | 2240 | SIZE_T cchString = 0; |
| 2211 | SIZE_T cchMultiSz = 0; | 2241 | SIZE_T cchMultiSz = 0; |
| 2212 | 2242 | ||
| 2243 | hr = ::StringCchLengthW(pwzInsert, STRSAFE_MAX_CCH, reinterpret_cast<size_t*>(&cchString)); | ||
| 2244 | StrExitOnRootFailure(hr, "failed to get length of insert string"); | ||
| 2245 | |||
| 2213 | if (pcchMultiSz && 0 != *pcchMultiSz) | 2246 | if (pcchMultiSz && 0 != *pcchMultiSz) |
| 2214 | { | 2247 | { |
| 2215 | cchMultiSz = *pcchMultiSz; | 2248 | cchMultiSz = *pcchMultiSz; |
| @@ -2464,11 +2497,15 @@ extern "C" HRESULT DAPI StrStringToInt64( | |||
| 2464 | INT iSign = 1; | 2497 | INT iSign = 1; |
| 2465 | INT nDigit = 0; | 2498 | INT nDigit = 0; |
| 2466 | LARGE_INTEGER liValue = { }; | 2499 | LARGE_INTEGER liValue = { }; |
| 2500 | size_t cchString = 0; | ||
| 2467 | 2501 | ||
| 2468 | // get string length if not provided | 2502 | // get string length if not provided |
| 2469 | if (0 >= cchIn) | 2503 | if (0 >= cchIn) |
| 2470 | { | 2504 | { |
| 2471 | cchIn = lstrlenW(wzIn); | 2505 | hr = ::StringCchLengthW(wzIn, STRSAFE_MAX_CCH, &cchString); |
| 2506 | StrExitOnRootFailure(hr, "Failed to get length of string."); | ||
| 2507 | |||
| 2508 | cchIn = (DWORD)cchString; | ||
| 2472 | if (0 >= cchIn) | 2509 | if (0 >= cchIn) |
| 2473 | { | 2510 | { |
| 2474 | ExitFunction1(hr = E_INVALIDARG); | 2511 | ExitFunction1(hr = E_INVALIDARG); |
| @@ -2524,11 +2561,15 @@ extern "C" HRESULT DAPI StrStringToUInt64( | |||
| 2524 | DWORD nDigit = 0; | 2561 | DWORD nDigit = 0; |
| 2525 | ULONGLONG ullValue = 0; | 2562 | ULONGLONG ullValue = 0; |
| 2526 | ULONGLONG ull = 0; | 2563 | ULONGLONG ull = 0; |
| 2564 | size_t cchString = 0; | ||
| 2527 | 2565 | ||
| 2528 | // get string length if not provided | 2566 | // get string length if not provided |
| 2529 | if (0 >= cchIn) | 2567 | if (0 >= cchIn) |
| 2530 | { | 2568 | { |
| 2531 | cchIn = lstrlenW(wzIn); | 2569 | hr = ::StringCchLengthW(wzIn, STRSAFE_MAX_CCH, &cchString); |
| 2570 | StrExitOnRootFailure(hr, "Failed to get length of string."); | ||
| 2571 | |||
| 2572 | cchIn = (DWORD)cchString; | ||
| 2532 | if (0 >= cchIn) | 2573 | if (0 >= cchIn) |
| 2533 | { | 2574 | { |
| 2534 | ExitFunction1(hr = E_INVALIDARG); | 2575 | ExitFunction1(hr = E_INVALIDARG); |
| @@ -2588,7 +2629,7 @@ StrAllocStringToUpperInvariant - creates an upper-case copy of a string. | |||
| 2588 | extern "C" HRESULT DAPI StrAllocStringToUpperInvariant( | 2629 | extern "C" HRESULT DAPI StrAllocStringToUpperInvariant( |
| 2589 | __deref_out_z LPWSTR* pscz, | 2630 | __deref_out_z LPWSTR* pscz, |
| 2590 | __in_z LPCWSTR wzSource, | 2631 | __in_z LPCWSTR wzSource, |
| 2591 | __in int cchSource | 2632 | __in SIZE_T cchSource |
| 2592 | ) | 2633 | ) |
| 2593 | { | 2634 | { |
| 2594 | return StrAllocStringMapInvariant(pscz, wzSource, cchSource, LCMAP_UPPERCASE); | 2635 | return StrAllocStringMapInvariant(pscz, wzSource, cchSource, LCMAP_UPPERCASE); |
| @@ -2601,7 +2642,7 @@ StrAllocStringToLowerInvariant - creates an lower-case copy of a string. | |||
| 2601 | extern "C" HRESULT DAPI StrAllocStringToLowerInvariant( | 2642 | extern "C" HRESULT DAPI StrAllocStringToLowerInvariant( |
| 2602 | __deref_out_z LPWSTR* pscz, | 2643 | __deref_out_z LPWSTR* pscz, |
| 2603 | __in_z LPCWSTR wzSource, | 2644 | __in_z LPCWSTR wzSource, |
| 2604 | __in int cchSource | 2645 | __in SIZE_T cchSource |
| 2605 | ) | 2646 | ) |
| 2606 | { | 2647 | { |
| 2607 | return StrAllocStringMapInvariant(pscz, wzSource, cchSource, LCMAP_LOWERCASE); | 2648 | return StrAllocStringMapInvariant(pscz, wzSource, cchSource, LCMAP_LOWERCASE); |
| @@ -2704,7 +2745,7 @@ Note: Assumes source and destination buffers will be the same. | |||
| 2704 | static HRESULT StrAllocStringMapInvariant( | 2745 | static HRESULT StrAllocStringMapInvariant( |
| 2705 | __deref_out_z LPWSTR* pscz, | 2746 | __deref_out_z LPWSTR* pscz, |
| 2706 | __in_z LPCWSTR wzSource, | 2747 | __in_z LPCWSTR wzSource, |
| 2707 | __in int cchSource, | 2748 | __in SIZE_T cchSource, |
| 2708 | __in DWORD dwMapFlags | 2749 | __in DWORD dwMapFlags |
| 2709 | ) | 2750 | ) |
| 2710 | { | 2751 | { |
| @@ -2718,11 +2759,15 @@ static HRESULT StrAllocStringMapInvariant( | |||
| 2718 | // Need the actual string size for LCMapString. This includes the null-terminator | 2759 | // Need the actual string size for LCMapString. This includes the null-terminator |
| 2719 | // but LCMapString doesn't care either way. | 2760 | // but LCMapString doesn't care either way. |
| 2720 | hr = ::StringCchLengthW(*pscz, INT_MAX, reinterpret_cast<size_t*>(&cchSource)); | 2761 | hr = ::StringCchLengthW(*pscz, INT_MAX, reinterpret_cast<size_t*>(&cchSource)); |
| 2721 | StrExitOnFailure(hr, "Failed to get the length of the string."); | 2762 | StrExitOnRootFailure(hr, "Failed to get the length of the string."); |
| 2763 | } | ||
| 2764 | else if (INT_MAX < cchSource) | ||
| 2765 | { | ||
| 2766 | StrExitOnRootFailure(hr = E_INVALIDARG, "Source string is too long: %Iu", cchSource); | ||
| 2722 | } | 2767 | } |
| 2723 | 2768 | ||
| 2724 | // Convert the copy of the string to upper or lower case in-place. | 2769 | // Convert the copy of the string to upper or lower case in-place. |
| 2725 | if (0 == ::LCMapStringW(LOCALE_INVARIANT, dwMapFlags, *pscz, cchSource, *pscz, cchSource)) | 2770 | if (0 == ::LCMapStringW(LOCALE_INVARIANT, dwMapFlags, *pscz, static_cast<int>(cchSource), *pscz, static_cast<int>(cchSource))) |
| 2726 | { | 2771 | { |
| 2727 | StrExitWithLastError(hr, "Failed to convert the string case."); | 2772 | StrExitWithLastError(hr, "Failed to convert the string case."); |
| 2728 | } | 2773 | } |
