From c294dd3142b10ab822982cda02728818c11f72da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefano=20Borz=C3=AC?= Date: Wed, 6 Nov 2019 09:34:24 +0100 Subject: [PATCH] fix(Core/Crash): Improved Utf8toWStr() function to prevent crashes (#2407) --- src/common/Utilities/Util.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index f2ad70a5a..8fed7c8cf 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -333,17 +333,14 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize) bool Utf8toWStr(const std::string& utf8str, std::wstring& wstr) { + wstr.clear(); try { - if (size_t len = utf8::distance(utf8str.c_str(), utf8str.c_str()+utf8str.size())) - { - wstr.resize(len); - utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), &wstr[0]); - } + utf8::utf8to16(utf8str.c_str(), utf8str.c_str()+utf8str.size(), std::back_inserter(wstr)); } - catch(std::exception) + catch(std::exception const&) { - wstr = L""; + wstr.clear(); return false; }