]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Fix bad conversion from UTF-16 in util::read_file on Windows (#1627)
authorautoantwort <41973254+autoantwort@users.noreply.github.com>
Wed, 3 Sep 2025 17:39:13 +0000 (19:39 +0200)
committerGitHub <noreply@github.com>
Wed, 3 Sep 2025 17:39:13 +0000 (19:39 +0200)
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 <Leander.Schulten@tetys.de>
src/ccache/util/file.cpp

index e8a9c3299ccc0bb83d0af9fbfdb32fca8bacf687..6605e96abb8ef4ffd687eb1043e2f929ccd98c24 100644 (file)
@@ -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<const wchar_t*>(result.c_str());
+      std::wstring result_as_u16(
+        reinterpret_cast<const wchar_t*>(result.data()), result.size() / 2);
       const int size = WideCharToMultiByte(CP_UTF8,
                                            WC_ERR_INVALID_CHARS,
                                            result_as_u16.c_str(),