diff options
author | John Chen <jochen@microsoft.com> | 2016-10-10 09:39:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-10 09:39:08 -0700 |
commit | e197c9e3bdc5f047689f69878fcc9ff72c438482 (patch) | |
tree | d048194e8c12a5a6204bd2f269655cc481380eea /src/utilcode | |
parent | edc6bf9d65e84afb6425cd23ee12ca18c5029398 (diff) | |
download | coreclr-e197c9e3bdc5f047689f69878fcc9ff72c438482.tar.gz coreclr-e197c9e3bdc5f047689f69878fcc9ff72c438482.tar.bz2 coreclr-e197c9e3bdc5f047689f69878fcc9ff72c438482.zip |
Fix buffer length usage in UTF-8 to Unicode conversion (#7540)
These changes are ported from desktop CLR.
Diffstat (limited to 'src/utilcode')
-rw-r--r-- | src/utilcode/fstring.cpp | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/utilcode/fstring.cpp b/src/utilcode/fstring.cpp index 33aa346a9c..1165ecaac0 100644 --- a/src/utilcode/fstring.cpp +++ b/src/utilcode/fstring.cpp @@ -73,6 +73,9 @@ HRESULT Unicode_Utf8_Length(__in_z LPCWSTR pString, __out bool * pAllAscii, __ou return HRESULT_FROM_GetLastError(); } + // Remove the count of null terminator, to be consistent with the all-ASCII case. + --*pLength; + if (*pLength > MAX_LENGTH) { return COR_E_OVERFLOW; @@ -129,7 +132,7 @@ HRESULT Unicode_Utf8(__in_z LPCWSTR pString, bool allAscii, __out_z LPSTR pBuffe } else { - length = WszWideCharToMultiByte(CP_UTF8, 0, pString, -1, pBuffer, (int) length, NULL, NULL); + length = WszWideCharToMultiByte(CP_UTF8, 0, pString, -1, pBuffer, (int) length + 1, NULL, NULL); if (length == 0) { @@ -190,6 +193,9 @@ HRESULT Utf8_Unicode_Length(__in_z LPCSTR pString, __out bool * pAllAscii, __out return HRESULT_FROM_GetLastError(); } + // Remove the count of null terminator, to be consistent with the all-ASCII case. + --*pLength; + if (* pLength > MAX_LENGTH) { return COR_E_OVERFLOW; @@ -200,7 +206,7 @@ HRESULT Utf8_Unicode_Length(__in_z LPCSTR pString, __out bool * pAllAscii, __out } -// UTF8 to ANSI +// UTF8 to Unicode HRESULT Utf8_Unicode(__in_z LPCSTR pString, bool allAscii, __out_z LPWSTR pBuffer, DWORD length) { @@ -247,7 +253,7 @@ HRESULT Utf8_Unicode(__in_z LPCSTR pString, bool allAscii, __out_z LPWSTR pBuffe } else { - length = WszMultiByteToWideChar(CP_UTF8, 0, pString, -1, pBuffer, (int) length); + length = WszMultiByteToWideChar(CP_UTF8, 0, pString, -1, pBuffer, (int) length + 1); if (length == 0) { |