From 169810e8f41e1f8064f2ce9be9df1be22c6c079e Mon Sep 17 00:00:00 2001 From: Shen-Ta Hsieh Date: Sat, 30 May 2020 04:01:23 +0800 Subject: [PATCH] lib/icu: add null terminate in win32 string and wstring --- src/lib/icu/Win32.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/icu/Win32.cxx b/src/lib/icu/Win32.cxx index c44d40a8f..f59de59d7 100644 --- a/src/lib/icu/Win32.cxx +++ b/src/lib/icu/Win32.cxx @@ -34,9 +34,10 @@ WideCharToMultiByte(unsigned code_page, std::wstring_view src) if (length <= 0) throw MakeLastError("Failed to convert from Unicode"); - std::unique_ptr buffer(new char[length]); + auto buffer = std::make_unique(length + 1); + buffer[length] = '\0'; length = WideCharToMultiByte(code_page, 0, src.data(), src.size(), - buffer.get(), length, + buffer.get(), length + 1, nullptr, nullptr); if (length <= 0) throw MakeLastError("Failed to convert from Unicode"); @@ -52,9 +53,10 @@ MultiByteToWideChar(unsigned code_page, std::string_view src) if (length <= 0) throw MakeLastError("Failed to convert to Unicode"); - std::unique_ptr buffer(new wchar_t[length]); + auto buffer = std::make_unique(length + 1); + buffer[length] = L'\0'; length = MultiByteToWideChar(code_page, 0, src.data(), src.size(), - buffer.get(), length); + buffer.get(), length + 1); if (length <= 0) throw MakeLastError("Failed to convert to Unicode");