From c8df2095319b99be099334c4889d57675a84cd4a Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Thu, 12 Mar 2020 20:06:13 -0500 Subject: [PATCH] Fix possible invalid output size in subsequent writes The fixed buffer size was being used rather than the number of bytes read in the latest round of UTF-8 conversion. --- paste/paste.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paste/paste.cpp b/paste/paste.cpp index 307ef22..3a8eb6d 100644 --- a/paste/paste.cpp +++ b/paste/paste.cpp @@ -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());