Skip to content

Commit

Permalink
Drop NULL byte from UTF-8 calculations and conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
mqudsi committed Mar 11, 2020
1 parent 0a07451 commit fd355a4
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions paste/paste.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ void Write(const wchar_t *text, DWORD outputHandle = STD_OUTPUT_HANDLE, DWORD ch
else
{
// WSL fakes the console and requires UTF-8 output
DWORD utf8ByteCount = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, chars + 1, nullptr, 0, nullptr, nullptr); // include null
DWORD utf8ByteCount = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, chars, nullptr, 0, nullptr, nullptr);
auto utf8Bytes = _malloc<char>(utf8ByteCount);
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, -1, utf8Bytes, utf8ByteCount, nullptr, nullptr);
result = WriteFile(hOut, utf8Bytes, utf8ByteCount - 1 /* remove null */, &charsWritten, nullptr);
if (charsWritten != utf8ByteCount - 1)
result = WriteFile(hOut, utf8Bytes, utf8ByteCount, &charsWritten, nullptr);
if (charsWritten != utf8ByteCount)
{
ExitProcess(GetLastError());
}
Expand Down

0 comments on commit fd355a4

Please sign in to comment.