diff --git a/AK/Utf16View.cpp b/AK/Utf16View.cpp index 6f0f4677714..de6ef1d546f 100644 --- a/AK/Utf16View.cpp +++ b/AK/Utf16View.cpp @@ -185,6 +185,8 @@ bool Utf16View::validate(size_t& valid_code_units, AllowLonelySurrogates allow_l size_t Utf16View::code_unit_offset_of(size_t code_point_offset) const { + VERIFY(code_point_offset <= length_in_code_points()); + if (length_in_code_points() == length_in_code_units()) // Fast path: all code points are one code unit. return code_point_offset; @@ -203,6 +205,8 @@ size_t Utf16View::code_unit_offset_of(size_t code_point_offset) const size_t Utf16View::code_point_offset_of(size_t code_unit_offset) const { + VERIFY(code_unit_offset <= length_in_code_units()); + if (length_in_code_points() == length_in_code_units()) // Fast path: all code points are one code unit. return code_unit_offset; diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 7d765c76032..ceeccfe2050 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -221,7 +221,10 @@ static ThrowCompletionOr regexp_builtin_exec(VM& vm, RegExpObject& regexp // ii. Set matchSucceeded to true. // 13.b and 13.c - regex.start_offset = full_unicode ? string->utf16_string_view().code_point_offset_of(last_index) : last_index; + regex.start_offset = full_unicode && last_index <= string->length_in_utf16_code_units() + ? string->utf16_string_view().code_point_offset_of(last_index) + : last_index; + result = regex.match(string->utf16_string_view()); // 13.d and 13.a