From 99afe8e6d1157c0d443ccbe4b571797cce5b2304 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 30 May 2020 13:27:46 +0200 Subject: [PATCH] lib/icu/Win32: paranoid-ify the buffer length checks Passing `length+1` to `MultiByteToWideChar()` means the function may fill the whole buffer with output data, and could theoretically overwrite the null terminator. In practice, this will never happen, but this way, it's slightly more correct. Also, null-terminate after `MultiByteToWideChar()`, after we got the real output length. Again, this would never have been a problem, but who knows... --- src/lib/icu/Win32.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/icu/Win32.cxx b/src/lib/icu/Win32.cxx index f59de59d7..e8a8b1086 100644 --- a/src/lib/icu/Win32.cxx +++ b/src/lib/icu/Win32.cxx @@ -35,13 +35,13 @@ WideCharToMultiByte(unsigned code_page, std::wstring_view src) throw MakeLastError("Failed to convert from Unicode"); auto buffer = std::make_unique(length + 1); - buffer[length] = '\0'; length = WideCharToMultiByte(code_page, 0, src.data(), src.size(), - buffer.get(), length + 1, + buffer.get(), length, nullptr, nullptr); if (length <= 0) throw MakeLastError("Failed to convert from Unicode"); + buffer[length] = '\0'; return AllocatedString::Donate(buffer.release()); } @@ -54,11 +54,11 @@ MultiByteToWideChar(unsigned code_page, std::string_view src) throw MakeLastError("Failed to convert to Unicode"); auto buffer = std::make_unique(length + 1); - buffer[length] = L'\0'; length = MultiByteToWideChar(code_page, 0, src.data(), src.size(), - buffer.get(), length + 1); + buffer.get(), length); if (length <= 0) throw MakeLastError("Failed to convert to Unicode"); + buffer[length] = L'\0'; return AllocatedString::Donate(buffer.release()); }