diff options
author | jackpoz <giacomopoz@gmail.com> | 2019-01-12 12:05:48 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2021-11-21 14:38:29 +0100 |
commit | e9e3d2d69dcabfcde325acae02d26c76cd6fab16 (patch) | |
tree | e4fbeab483f0904e930160ca17be63a968a59d2c | |
parent | d8b76a6e6d03b731ad89ea2e91ea93852a44162f (diff) |
Core/Utils: Handle UTF-8 conversion errors
Replace the output string when a UTF-8 conversion error happen with an error message instead of using an empty string, swallowing any message that the caller wanted to log.
(cherry picked from commit bdb7e6e5e81e0d9376c9c803bae9da470b02d8cd)
-rw-r--r-- | src/common/Utilities/Util.cpp | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 099de72c7af..f787edd13ea 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -258,9 +258,23 @@ bool Utf8toWStr(char const* utf8str, size_t csize, wchar_t* wstr, size_t& wsize) } catch (std::exception const&) { - if (wsize > 0) + // Replace the converted string with an error message if there is enough space + // Otherwise just return an empty string + wchar_t const* errorMessage = L"An error occurred converting string from UTF-8 to WStr"; + size_t errorMessageLength = wcslen(errorMessage); + if (wsize >= errorMessageLength) + { + wcscpy(wstr, errorMessage); + wsize = wcslen(wstr); + } + else if (wsize > 0) + { wstr[0] = L'\0'; - wsize = 0; + wsize = 0; + } + else + wsize = 0; + return false; } |