(Windows) Added error handling to StringUtil::stringToWideString() and StringUtil::wideStringToString() to avoid crashes caused by invalid characters.

This commit is contained in:
Leon Styhre 2022-08-01 19:51:25 +02:00
parent 56665af45e
commit f550ba7dfc

View file

@ -12,6 +12,7 @@
#endif
#include "utils/StringUtil.h"
#include "Log.h"
#include <algorithm>
#include <locale>
@ -617,14 +618,28 @@ namespace Utils
std::wstring stringToWideString(const std::string& stringArg)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> stringConverter;
try {
return stringConverter.from_bytes(stringArg);
}
catch (...) {
LOG(LogError) << "StringUtil::stringToWideString(): Conversion failed, invalid "
"characters in source string?";
return L"";
}
}
std::string wideStringToString(const std::wstring& stringArg)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> stringConverter;
try {
return stringConverter.to_bytes(stringArg);
}
catch (...) {
LOG(LogError) << "StringUtil::wideStringToString(): Conversion failed, invalid "
"characters in source string?";
return "";
}
}
bool startsWith(const std::string& stringArg, const std::string& start)
{