From 61c3d356346587bc1fbc05d3633d10de2d2acdc8 Mon Sep 17 00:00:00 2001 From: Daniela Engert Date: Sun, 27 Jan 2019 23:08:01 +0100 Subject: [PATCH] Fix problem with wchar_t* to std::string conversion. (#419) VS2019 correctly points out that a conversion from a wide NTCS to std::string requires narrowing. This may result in an undesired string value. Implement the string conversion properly. Signed-off-by: Daniela Engert --- src/tz.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/tz.cpp b/src/tz.cpp index 7b9eb10..5e538da 100644 --- a/src/tz.cpp +++ b/src/tz.cpp @@ -98,6 +98,7 @@ #include #include #include +#include #include #include #include @@ -208,7 +209,14 @@ get_known_folder(const GUID& folderid) if (SUCCEEDED(hr)) { co_task_mem_ptr folder_ptr(pfolder); - folder = std::string(folder_ptr.get(), folder_ptr.get() + wcslen(folder_ptr.get())); + const wchar_t* fptr = folder_ptr.get(); + auto state = std::mbstate_t(); + const auto required = std::wcsrtombs(nullptr, &fptr, 0, &state); + if (required != 0 && required != -1) + { + folder.resize(required); + std::wcsrtombs(folder.data(), &fptr, folder.size(), &state); + } } return folder; }