[Support] Correctly handle zero length inputs to UTF conversion functions on Windows.

llvm-svn: 201811
This commit is contained in:
Michael J. Spencer 2014-02-20 20:46:23 +00:00
parent 2350c867cb
commit 7a3a51053d
1 changed files with 26 additions and 28 deletions

View File

@ -1040,22 +1040,22 @@ bool home_directory(SmallVectorImpl<char> &result) {
namespace windows { namespace windows {
llvm::error_code UTF8ToUTF16(llvm::StringRef utf8, llvm::error_code UTF8ToUTF16(llvm::StringRef utf8,
llvm::SmallVectorImpl<wchar_t> &utf16) { llvm::SmallVectorImpl<wchar_t> &utf16) {
int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, if (!utf8.empty()) {
utf8.begin(), utf8.size(), int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
utf16.begin(), 0); utf8.size(), utf16.begin(), 0);
if (len == 0) if (len == 0)
return llvm::windows_error(::GetLastError()); return llvm::windows_error(::GetLastError());
utf16.reserve(len + 1); utf16.reserve(len + 1);
utf16.set_size(len); utf16.set_size(len);
len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
utf8.begin(), utf8.size(), utf8.size(), utf16.begin(), utf16.size());
utf16.begin(), utf16.size());
if (len == 0) if (len == 0)
return llvm::windows_error(::GetLastError()); return llvm::windows_error(::GetLastError());
}
// Make utf16 null terminated. // Make utf16 null terminated.
utf16.push_back(0); utf16.push_back(0);
@ -1066,26 +1066,24 @@ llvm::error_code UTF8ToUTF16(llvm::StringRef utf8,
llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
llvm::SmallVectorImpl<char> &utf8) { llvm::SmallVectorImpl<char> &utf8) {
// Get length. if (utf16_len) {
int len = ::WideCharToMultiByte(CP_UTF8, 0, // Get length.
utf16, utf16_len, int len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.begin(),
utf8.begin(), 0, 0, NULL, NULL);
NULL, NULL);
if (len == 0) if (len == 0)
return llvm::windows_error(::GetLastError()); return llvm::windows_error(::GetLastError());
utf8.reserve(len); utf8.reserve(len);
utf8.set_size(len); utf8.set_size(len);
// Now do the actual conversion. // Now do the actual conversion.
len = ::WideCharToMultiByte(CP_UTF8, 0, len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.data(),
utf16, utf16_len, utf8.size(), NULL, NULL);
utf8.data(), utf8.size(),
NULL, NULL);
if (len == 0) if (len == 0)
return llvm::windows_error(::GetLastError()); return llvm::windows_error(::GetLastError());
}
// Make utf8 null terminated. // Make utf8 null terminated.
utf8.push_back(0); utf8.push_back(0);