From 5673e9e545df3495b0775cdc54c2c899517ee2a5 Mon Sep 17 00:00:00 2001 From: captainurist <73941350+captainurist@users.noreply.github.com> Date: Wed, 6 Nov 2024 04:54:01 +0800 Subject: [PATCH 1/2] utf8_to_wstrbuf now handles invalid utf8 sequences (#3244) --- include/spdlog/details/os-inl.h | 6 +++--- tests/includes.h | 1 + tests/test_misc.cpp | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index e3c80b92..e4d4771b 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -483,12 +483,12 @@ SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { // find the size to allocate for the result buffer int result_size = - ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0); + ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0); if (result_size > 0) { target.resize(result_size); - result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, - target.data(), result_size); + result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), + result_size); if (result_size > 0) { assert(result_size == target.size()); return; diff --git a/tests/includes.h b/tests/includes.h index 5dbb2191..2e49a5cb 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -26,6 +26,7 @@ #include "spdlog/spdlog.h" #include "spdlog/async.h" #include "spdlog/details/fmt_helper.h" +#include "spdlog/details/os.h" #ifndef SPDLOG_NO_TLS #include "spdlog/mdc.h" diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index fb8c9014..dfff2607 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -167,3 +167,21 @@ TEST_CASE("default logger API", "[default logger]") { spdlog::drop_all(); spdlog::set_pattern("%v"); } + +#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) +TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") { + spdlog::wmemory_buf_t buffer; + + spdlog::details::os::utf8_to_wstrbuf("", buffer); + REQUIRE(buffer.data() == std::wstring(L"")); + + spdlog::details::os::utf8_to_wstrbuf("abc", buffer); + REQUIRE(buffer.data() == std::wstring(L"abc")); + + spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence. + REQUIRE(buffer.data() == std::wstring(L"\xfffd(")); + + spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana. + REQUIRE(buffer.data() == std::wstring(L"\x306d\x3053")); +} +#endif From fe4f99527d926b4d97644e14da49d1cf5c817d4f Mon Sep 17 00:00:00 2001 From: captainurist <73941350+captainurist@users.noreply.github.com> Date: Wed, 6 Nov 2024 06:08:36 +0800 Subject: [PATCH 2/2] Fix utf8_to_wstrbuf tests (#3245) --- tests/test_misc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_misc.cpp b/tests/test_misc.cpp index dfff2607..5ae3c045 100644 --- a/tests/test_misc.cpp +++ b/tests/test_misc.cpp @@ -173,15 +173,15 @@ TEST_CASE("utf8 to utf16 conversion using windows api", "[windows utf]") { spdlog::wmemory_buf_t buffer; spdlog::details::os::utf8_to_wstrbuf("", buffer); - REQUIRE(buffer.data() == std::wstring(L"")); + REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"")); spdlog::details::os::utf8_to_wstrbuf("abc", buffer); - REQUIRE(buffer.data() == std::wstring(L"abc")); + REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"abc")); spdlog::details::os::utf8_to_wstrbuf("\xc3\x28", buffer); // Invalid UTF-8 sequence. - REQUIRE(buffer.data() == std::wstring(L"\xfffd(")); + REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\xfffd(")); spdlog::details::os::utf8_to_wstrbuf("\xe3\x81\xad\xe3\x81\x93", buffer); // "Neko" in hiragana. - REQUIRE(buffer.data() == std::wstring(L"\x306d\x3053")); + REQUIRE(std::wstring(buffer.data(), buffer.size()) == std::wstring(L"\x306d\x3053")); } #endif