Repository: LadybirdBrowser/ladybird
File: Libraries/LibRegex/RegexByteCode.cpp (C++)

Code before:
            auto input_view = input.view.substring_view(state.string_position - 1, 1).code_point_at(0);
            return input_view == '\r' || input_view == '\n' || input_view == LineSeparator || input_view == ParagraphSeparator;
        }

        return false;
    }();
    if (is_at_line_boundary && (input.regex_options & AllFlags::MatchNotBeginOfLine))
        return ExecutionResult::Failed_ExecuteLowPrioForks;

    if ((is_at_line_boundary && !(input.regex_options & AllFlags::MatchNotBeginOfLine))
        || (!is_at_line_boundary && (input.regex_options & AllFlags::MatchNotBeginOfLine))
        || (is_at_line_boundary && (input.regex_options & AllFlags::Global)))
        return ExecutionResult::Continue;

    return ExecutionResult::Failed_ExecuteLowPrioForks;
}

ALWAYS_INLINE ExecutionResult OpCode_CheckBoundary::execute(MatchInput const& input, MatchState& state) const
{
    auto isword = [](auto ch) { return is_ascii_alphanumeric(ch) || ch == '_'; };
    auto is_word_boundary = [&] {
        size_t prev_pos = state.string_position_in_code_units - 1;

        if (state.stepBacks.size() > 0) {
            prev_pos = state.string_position_in_code_units + 1;
        }

        if (state.string_position == input.view.length()) {
            return (state.string_position > 0 && isword(input.view.code_point_at(prev_pos)));
        }

        if (state.string_position == 0) {
            return (isword(input.view.code_point_at(0)));
        }

        return !!(isword(input.view.code_point_at(state.string_position_in_code_units)) ^ isword(input.view.code_point_at(prev_pos)));
    };
    switch (type()) {
    case BoundaryCheckType::Word: {
        if (is_word_boundary())
            return ExecutionResult::Continue;
        return ExecutionResult::Failed_ExecuteLowPrioForks;
    }
    case BoundaryCheckType::NonWord: {
        if (!is_word_boundary())
            return ExecutionResult::Continue;
        return ExecutionResult::Failed_ExecuteLowPrioForks;
    }
    }
    VERIFY_NOT_REACHED();
}

Review comment (suggestion):
I don't quite understand the logic, but we can go out of bounds because of +1, right?
Here, `/(?<=\b)\b/` on 'ab' for example:
`string_position_in_code_units` will go from 0 to 1, then we'll set `prev_pos` to 1 + 1 and later attempt to `input.view.code_point_at(prev_pos)` on input with a length of 2

Code after:
    state.fork_at_position = state.instruction_position + size() + offset();
    input.fork_to_replace = state.instruction_position;
    return ExecutionResult::Fork_PrioLow;
}

template<typename ByteCode>
ALWAYS_INLINE ExecutionResult OpCode_ForkIf<ByteCode>::execute(MatchInput const& input, MatchState& state) const
{
    auto next_step = [&](bool do_fork) -> ExecutionResult {
        switch (form()) {
        case OpCodeId::ForkJump:
            if (do_fork) {
                state.fork_at_position = state.instruction_position + size() + offset();
                state.forks_since_last_save++;
                return ExecutionResult::Fork_PrioHigh;
            }
            return ExecutionResult::Continue;
        case OpCodeId::ForkReplaceJump:
            if (do_fork) {
                state.fork_at_position = state.instruction_position + size() + offset();
                input.fork_to_replace = state.instruction_position;
                state.forks_since_last_save++;
                return ExecutionResult::Fork_PrioHigh;
            }
            return ExecutionResult::Continue;
        case OpCodeId::ForkStay:
            if (do_fork) {
                state.fork_at_position = state.instruction_position + size() + offset();
                state.forks_since_last_save++;
                return ExecutionResult::Fork_PrioLow;
            }
            state.instruction_position += offset();
            return ExecutionResult::Continue;
        case OpCodeId::ForkReplaceStay:
            if (do_fork) {
                state.fork_at_position = state.instruction_position + size() + offset();
                input.fork_to_replace = state.instruction_position;
                return ExecutionResult::Fork_PrioLow;
            }
            state.instruction_position += offset();
            return ExecutionResult::Continue;
        default:
            VERIFY_NOT_REACHED();
        }
    };

    switch (condition()) {
    case ForkIfCondition::AtStartOfLine:
        return next_step(!input.in_the_middle_of_a_line);
    case ForkIfCondition::Invalid:
    default: