From: autoantwort <41973254+autoantwort@users.noreply.github.com> Date: Wed, 3 Sep 2025 17:39:13 +0000 (+0200) Subject: fix: Fix bad conversion from UTF-16 in util::read_file on Windows (#1627) X-Git-Tag: v4.12~22 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aad12ffa329d2d8ca71857e041a0584946e4f5de;p=thirdparty%2Fccache.git fix: Fix bad conversion from UTF-16 in util::read_file on Windows (#1627) When interpreting a char* string that is null terminated as wchar* string it is not guaranteed that the wchar* string is also null terminated. A char[3] string/memeory region with the content ['a', '\0', '\0', 100] interpreted as wchar* has no ending null character resulting in out of bounds memory reads when trying to construct a std::wstring from it. Co-authored-by: Leander Schulten --- diff --git a/src/ccache/util/file.cpp b/src/ccache/util/file.cpp index e8a9c329..6605e96a 100644 --- a/src/ccache/util/file.cpp +++ b/src/ccache/util/file.cpp @@ -387,8 +387,8 @@ read_file(const fs::path& path, size_t size_hint) return result; } - std::wstring result_as_u16((result.size() / 2) + 1, '\0'); - result_as_u16 = reinterpret_cast(result.c_str()); + std::wstring result_as_u16( + reinterpret_cast(result.data()), result.size() / 2); const int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, result_as_u16.c_str(),