From: Joel Rosdahl Date: Sat, 19 Mar 2022 19:23:21 +0000 (+0100) Subject: fix: Work around endianness problem in Util::read_text_file X-Git-Tag: v4.6.1~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dfb3111f71ea19e7d2c3aaf161ba96a44e2af155;p=thirdparty%2Fccache.git fix: Work around endianness problem in Util::read_text_file The code in Util::read_text_file for converting UTF-16LE to UTF-8 only works on little-endian machines. This makes the unit test fail on big-endian machines. Since the conversion is only needed on Windows (for Visual Studio, which creates UTF-16LE .rsp files) in practice, work around the problem by only doing the conversion in Windows builds. Fixes #1014. --- diff --git a/src/Util.cpp b/src/Util.cpp index 146ae7a9a..0a4d43041 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -209,6 +209,7 @@ rewrite_stderr_to_absolute_paths(string_view text) return result; } +#ifdef _WIN32 bool has_utf16_le_bom(string_view text) { @@ -216,6 +217,7 @@ has_utf16_le_bom(string_view text) && ((static_cast(text[0]) == 0xff && static_cast(text[1]) == 0xfe)); } +#endif } // namespace @@ -1190,7 +1192,12 @@ std::string read_text_file(const std::string& path, size_t size_hint) { std::string result = read_file(path, size_hint); +#ifdef _WIN32 // Convert to UTF-8 if the content starts with a UTF-16 little-endian BOM. + // + // Note that this code assumes a little-endian machine, which is why it's + // #ifdef-ed to only run on Windows (which is always little-endian) where it's + // actually needed. if (has_utf16_le_bom(result)) { result.erase(0, 2); // Remove BOM. std::u16string result_as_u16((result.size() / 2) + 1, '\0'); @@ -1198,6 +1205,7 @@ read_text_file(const std::string& path, size_t size_hint) std::wstring_convert, char16_t> converter; result = converter.to_bytes(result_as_u16); } +#endif return result; } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 913b73624..4ea9903e5 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -689,6 +689,7 @@ TEST_CASE("Util::{read,write,copy}_file with binary files") CHECK(Util::read_file("copy") == data); } +#ifdef _WIN32 TEST_CASE("Util::read_text_file with UTF-16 little endian encoding") { TestContext test_context; @@ -706,6 +707,7 @@ TEST_CASE("Util::read_text_file with UTF-16 little endian encoding") Util::write_file("test", data); CHECK(Util::read_text_file("test") == "abc"); } +#endif TEST_CASE("Util::remove_extension") {