diff options
author | Andreas Kling <kling@serenityos.org> | 2023-02-18 13:43:52 +0100 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2023-02-18 09:12:46 -0500 |
commit | d0697d350d43d42991b0bd47ee834f6c0c8b9d3a (patch) | |
tree | 49f85802ad0b83c7fdc4ac768035c9b48968d268 | |
parent | ba5df46a49b3f026e67924fb262db81ce311e8f6 (diff) | |
download | serenity-d0697d350d43d42991b0bd47ee834f6c0c8b9d3a.zip |
AK: Fix 64-bit alignment issue in shared-superstring substrings
Thanks to Timothy Flynn for the test!
Fixes #17141
-rw-r--r-- | AK/String.cpp | 2 | ||||
-rw-r--r-- | Tests/AK/TestString.cpp | 11 |
2 files changed, 12 insertions, 1 deletions
diff --git a/AK/String.cpp b/AK/String.cpp index 49aed1d247..d1dc6d2e9c 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -77,7 +77,7 @@ private: bool m_substring { false }; bool m_is_fly_string { false }; - u8 m_bytes_or_substring_data[0]; + alignas(SubstringData) u8 m_bytes_or_substring_data[0]; }; void StringData::operator delete(void* ptr) diff --git a/Tests/AK/TestString.cpp b/Tests/AK/TestString.cpp index 6c40eba488..ae48664b83 100644 --- a/Tests/AK/TestString.cpp +++ b/Tests/AK/TestString.cpp @@ -97,6 +97,17 @@ TEST_CASE(substring) EXPECT_EQ(long_substring, "Hello I am"sv); } +TEST_CASE(substring_with_shared_superstring) +{ + auto superstring = MUST(String::from_utf8("Hello I am a long string"sv)); + + auto substring1 = MUST(superstring.substring_from_byte_offset_with_shared_superstring(0, 5)); + EXPECT_EQ(substring1, "Hello"sv); + + auto substring2 = MUST(superstring.substring_from_byte_offset_with_shared_superstring(0, 10)); + EXPECT_EQ(substring2, "Hello I am"sv); +} + TEST_CASE(code_points) { auto string = MUST(String::from_utf8("🦬🪒"sv)); |