From 225d1f3c6a0c15b679308b2813c499f892499a13 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Thu, 20 Feb 2014 20:46:23 +0000 Subject: [PATCH] [Support] Correctly handle zero length inputs to UTF conversion functions on Windows. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201811 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Windows/Path.inc | 54 +++++++++++++++++------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc index 4c51c4401f8..fa7d18a711a 100644 --- a/lib/Support/Windows/Path.inc +++ b/lib/Support/Windows/Path.inc @@ -1040,22 +1040,22 @@ bool home_directory(SmallVectorImpl &result) { namespace windows { llvm::error_code UTF8ToUTF16(llvm::StringRef utf8, llvm::SmallVectorImpl &utf16) { - int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - utf8.begin(), utf8.size(), - utf16.begin(), 0); + if (!utf8.empty()) { + int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(), + utf8.size(), utf16.begin(), 0); - if (len == 0) - return llvm::windows_error(::GetLastError()); + if (len == 0) + return llvm::windows_error(::GetLastError()); - utf16.reserve(len + 1); - utf16.set_size(len); + utf16.reserve(len + 1); + utf16.set_size(len); - len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - utf8.begin(), utf8.size(), - utf16.begin(), utf16.size()); + len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(), + utf8.size(), utf16.begin(), utf16.size()); - if (len == 0) - return llvm::windows_error(::GetLastError()); + if (len == 0) + return llvm::windows_error(::GetLastError()); + } // Make utf16 null terminated. 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::SmallVectorImpl &utf8) { - // Get length. - int len = ::WideCharToMultiByte(CP_UTF8, 0, - utf16, utf16_len, - utf8.begin(), 0, - NULL, NULL); + if (utf16_len) { + // Get length. + int len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.begin(), + 0, NULL, NULL); - if (len == 0) - return llvm::windows_error(::GetLastError()); + if (len == 0) + return llvm::windows_error(::GetLastError()); - utf8.reserve(len); - utf8.set_size(len); + utf8.reserve(len); + utf8.set_size(len); - // Now do the actual conversion. - len = ::WideCharToMultiByte(CP_UTF8, 0, - utf16, utf16_len, - utf8.data(), utf8.size(), - NULL, NULL); + // Now do the actual conversion. + len = ::WideCharToMultiByte(CP_UTF8, 0, utf16, utf16_len, utf8.data(), + utf8.size(), NULL, NULL); - if (len == 0) - return llvm::windows_error(::GetLastError()); + if (len == 0) + return llvm::windows_error(::GetLastError()); + } // Make utf8 null terminated. utf8.push_back(0);