Skip to content

Commit

Permalink
Fix possible invalid output size in subsequent writes
Browse files Browse the repository at this point in the history
The fixed buffer size was being used rather than the number of bytes read in
the latest round of UTF-8 conversion.
  • Loading branch information
mqudsi committed Mar 13, 2020
1 parent 88f84a3 commit c8df209
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions paste/paste.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ void Write(const wchar_t *text, DWORD outputHandle = STD_OUTPUT_HANDLE, DWORD ch
_utf8Buffer.Length = utf8ByteCount;
}
// "WideCharToMultiByte function operates most efficiently when both lpDefaultChar and lpUsedDefaultChar are set to NULL."
int bytesConverted = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, chars, _utf8Buffer.Buffer, _utf8Buffer.Length, nullptr, nullptr);
result = WriteFile(hOut, _utf8Buffer.Buffer, _utf8Buffer.Length, &charsWritten, nullptr);
DWORD bytesConverted = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, text, chars, _utf8Buffer.Buffer, _utf8Buffer.Length, nullptr, nullptr);
result = WriteFile(hOut, _utf8Buffer.Buffer, bytesConverted, &charsWritten, nullptr);
if (charsWritten != utf8ByteCount)
{
ExitProcess(GetLastError());
Expand Down

0 comments on commit c8df209

Please sign in to comment.