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

Gao, Yunzhong yunzhong_gao at playstation.sony.com
Thu Feb 20 16:46:56 PST 2014


Hi Rafael,

With this patch, UTF8ToUTF16 and UTF16ToUTF8 will convert an empty string back to an empty string instead of returning
an error, which seems like a reasonable thing to do.

I do have a script that demonstrates the problem, but I am not sure whether it is possible to convert it into a regression test
case (Any advice on this?). Without Mike's patch, the compiler will issue an "Parameter is incorrect" error. The same script
worked with 3.3 but not with current trunk or 3.4.

- Gao.

# test.py
# To use the script, edit the compiler and input_file strings in the file, and then do "python test.py"
import sys
import subprocess

def test():
  compiler = "C:\\clang.exe"
  input_file = "hello_world.c"
  # It is two single quotes between compiler and -O2. If I remove the two single quotes, the compilation works.
  command = [compiler, '', '-O2', '-g', input_file]
  print command
  subprocess.Popen(command)

test()
# end test.py


________________________________________
From: llvm-commits-bounces at cs.uiuc.edu [llvm-commits-bounces at cs.uiuc.edu] on behalf of Rafael EspĂ­ndola [rafael.espindola at gmail.com]
Sent: Thursday, February 20, 2014 2:31 PM
To: Michael J. Spencer
Cc: llvm-commits
Subject: Re: [llvm] r201811 - [Support] Correctly handle zero length inputs to  UTF conversion functions on Windows.

Is this testable? What was the issue, returning an error on every empty string?

On 20 February 2014 15:46, Michael J. Spencer <bigcheesegs at gmail.com> wrote:
> Author: mspencer
> Date: Thu Feb 20 14:46:23 2014
> New Revision: 201811
>
> URL: http://llvm.org/viewvc/llvm-project?rev=201811&view=rev
> Log:
> [Support] Correctly handle zero length inputs to UTF conversion functions on Windows.
>
> Modified:
>     llvm/trunk/lib/Support/Windows/Path.inc
>
> Modified: llvm/trunk/lib/Support/Windows/Path.inc
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Path.inc?rev=201811&r1=201810&r2=201811&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Support/Windows/Path.inc (original)
> +++ llvm/trunk/lib/Support/Windows/Path.inc Thu Feb 20 14:46:23 2014
> @@ -1040,22 +1040,22 @@ bool home_directory(SmallVectorImpl<char
>  namespace windows {
>  llvm::error_code UTF8ToUTF16(llvm::StringRef utf8,
>                               llvm::SmallVectorImpl<wchar_t> &utf16) {
> -  int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
> -                                  utf8.begin(), utf8.size(),
> -                                  utf16.begin(), 0);
> -
> -  if (len == 0)
> -    return llvm::windows_error(::GetLastError());
> -
> -  utf16.reserve(len + 1);
> -  utf16.set_size(len);
> -
> -  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 (!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());
> +
> +    utf16.reserve(len + 1);
> +    utf16.set_size(len);
> +
> +    len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, utf8.begin(),
> +                                utf8.size(), utf16.begin(), utf16.size());
> +
> +    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::Strin
>
>  llvm::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
>                               llvm::SmallVectorImpl<char> &utf8) {
> -  // Get length.
> -  int len = ::WideCharToMultiByte(CP_UTF8, 0,
> -                                  utf16, utf16_len,
> -                                  utf8.begin(), 0,
> -                                  NULL, NULL);
> -
> -  if (len == 0)
> -    return llvm::windows_error(::GetLastError());
> -
> -  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);
> -
> -  if (len == 0)
> -    return llvm::windows_error(::GetLastError());
> +  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());
> +
> +    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);
> +
> +    if (len == 0)
> +      return llvm::windows_error(::GetLastError());
> +  }
>
>    // Make utf8 null terminated.
>    utf8.push_back(0);
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
_______________________________________________
llvm-commits mailing list
llvm-commits at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits






More information about the llvm-commits mailing list