mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-04 01:12:56 +00:00
AK: Explicitly check for null data in Utf8View
The underlying CPU-specific instructions for operating on UTF-8 strings behave differently for null inputs. Add an explicit check for this state for consistency.
This commit is contained in:
parent
ac6126e263
commit
144452d638
Notes:
github-actions[bot]
2024-07-21 17:58:05 +00:00
Author: https://github.com/trflynn89
Commit: 144452d638
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/734
Reviewed-by: https://github.com/jamierocks ✅
Reviewed-by: https://github.com/tcl3 ✅
2 changed files with 22 additions and 0 deletions
|
@ -76,6 +76,10 @@ Utf8View Utf8View::unicode_substring_view(size_t code_point_offset, size_t code_
|
||||||
|
|
||||||
size_t Utf8View::calculate_length() const
|
size_t Utf8View::calculate_length() const
|
||||||
{
|
{
|
||||||
|
// FIXME: The CPU-specific implementations behave differently on null inputs. We treat null views as an empty string.
|
||||||
|
if (is_empty())
|
||||||
|
return 0;
|
||||||
|
|
||||||
// FIXME: simdutf's code point length method assumes valid UTF-8, whereas Utf8View uses U+FFFD as a replacement
|
// FIXME: simdutf's code point length method assumes valid UTF-8, whereas Utf8View uses U+FFFD as a replacement
|
||||||
// for invalid code points. If we change Utf8View to only accept valid encodings as an invariant, we can
|
// for invalid code points. If we change Utf8View to only accept valid encodings as an invariant, we can
|
||||||
// remove this branch.
|
// remove this branch.
|
||||||
|
@ -155,6 +159,12 @@ Utf8View Utf8View::trim(Utf8View const& characters, TrimMode mode) const
|
||||||
|
|
||||||
bool Utf8View::validate(size_t& valid_bytes, AllowSurrogates allow_surrogates) const
|
bool Utf8View::validate(size_t& valid_bytes, AllowSurrogates allow_surrogates) const
|
||||||
{
|
{
|
||||||
|
// FIXME: The CPU-specific implementations behave differently on null inputs. We treat null views as an empty string.
|
||||||
|
if (is_empty()) {
|
||||||
|
valid_bytes = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
auto result = simdutf::validate_utf8_with_errors(m_string.characters_without_null_termination(), m_string.length());
|
auto result = simdutf::validate_utf8_with_errors(m_string.characters_without_null_termination(), m_string.length());
|
||||||
valid_bytes = result.count;
|
valid_bytes = result.count;
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,18 @@ TEST_CASE(decode_utf8)
|
||||||
EXPECT_EQ(i, expected_size);
|
EXPECT_EQ(i, expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(null_view)
|
||||||
|
{
|
||||||
|
Utf8View view;
|
||||||
|
EXPECT(view.validate(Utf8View::AllowSurrogates::No));
|
||||||
|
EXPECT(view.validate(Utf8View::AllowSurrogates::Yes));
|
||||||
|
EXPECT_EQ(view.byte_length(), 0zu);
|
||||||
|
EXPECT_EQ(view.length(), 0zu);
|
||||||
|
|
||||||
|
for ([[maybe_unused]] auto it : view)
|
||||||
|
FAIL("Iterating a null UTF-8 string should not produce any values");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE(validate_invalid_ut8)
|
TEST_CASE(validate_invalid_ut8)
|
||||||
{
|
{
|
||||||
size_t valid_bytes;
|
size_t valid_bytes;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue