Skip to content

Commit d950250

Browse files
trflynn89gmta
authored andcommitted
AK: Fix bounds assertions in Utf16View::iterator_offset
1 parent 67723ef commit d950250

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

AK/Utf16View.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ class Utf16View {
366366
if (has_ascii_storage()) {
367367
VERIFY(it.has_ascii_storage());
368368
VERIFY(it.m_iterator.ascii >= m_string.ascii);
369-
VERIFY(it.m_iterator.ascii <= m_string.ascii);
369+
VERIFY(it.m_iterator.ascii <= m_string.ascii + length_in_code_units());
370370

371371
return it.m_iterator.ascii - m_string.ascii;
372372
}
373373

374374
VERIFY(!it.has_ascii_storage());
375375
VERIFY(it.m_iterator.utf16 >= m_string.utf16);
376-
VERIFY(it.m_iterator.utf16 <= m_string.utf16);
376+
VERIFY(it.m_iterator.utf16 <= m_string.utf16 + length_in_code_units());
377377

378378
return it.m_iterator.utf16 - m_string.utf16;
379379
}

Tests/AK/TestUtf16View.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,19 @@ TEST_CASE(code_point_offset_of)
495495
EXPECT_EQ(view.code_point_offset_of(13), 11uz);
496496
}
497497

498+
TEST_CASE(iterator_offset)
499+
{
500+
Utf16View view { u"😂 foo 😀 bar"sv };
501+
size_t expected_offset = 0;
502+
503+
for (auto it = view.begin(); it != view.end(); ++it) {
504+
EXPECT_EQ(view.iterator_offset(it), expected_offset);
505+
expected_offset += it.length_in_code_units();
506+
}
507+
508+
EXPECT_EQ(view.iterator_offset(view.end()), view.length_in_code_units());
509+
}
510+
498511
TEST_CASE(replace)
499512
{
500513
auto result = u""sv.replace({}, {}, ReplaceMode::FirstOnly);

0 commit comments

Comments
 (0)