1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:07:35 +00:00

LibRegex: Correctly handle failing in the middle of explicit repeats

- Make sure that all the Repeat ops are reset (otherwise the operation
  would not be correct when going over the Repeat op a second time)
- Make sure that all matches that are allowed to fail are backed by a
  fork, otherwise the last failing fork would not have anywhere to
  return to.
Fixes #9707.
This commit is contained in:
Ali Mohammad Pur 2021-08-31 22:27:08 +04:30 committed by Andreas Kling
parent fcdd7aa990
commit dd82c2e9b4
3 changed files with 59 additions and 24 deletions

View file

@ -446,8 +446,9 @@ bool PosixBasicParser::parse_simple_re(ByteCode& bytecode, size_t& match_length_
if (min_limit > s_maximum_repetition_count || (max_limit.has_value() && *max_limit > s_maximum_repetition_count))
return set_error(Error::InvalidBraceContent);
auto repetition_mark_id = m_parser_state.repetition_mark_count++;
ByteCode::transform_bytecode_repetition_min_max(simple_re_bytecode, min_limit, max_limit, repetition_mark_id, true);
auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
ByteCode::transform_bytecode_repetition_min_max(simple_re_bytecode, min_limit, max_limit, min_repetition_mark_id, max_repetition_mark_id, true);
match_length_minimum += re_match_length_minimum * min_limit;
} else {
match_length_minimum += re_match_length_minimum;
@ -620,8 +621,9 @@ ALWAYS_INLINE bool PosixExtendedParser::parse_repetition_symbol(ByteCode& byteco
maybe_maximum = value.value();
}
auto repetition_mark_id = m_parser_state.repetition_mark_count++;
ByteCode::transform_bytecode_repetition_min_max(bytecode_to_repeat, minimum, maybe_maximum, repetition_mark_id);
auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
ByteCode::transform_bytecode_repetition_min_max(bytecode_to_repeat, minimum, maybe_maximum, min_repetition_mark_id, max_repetition_mark_id);
consume(TokenType::RightCurly, Error::MismatchingBrace);
return !has_error();
@ -1219,8 +1221,9 @@ bool ECMA262Parser::parse_quantifier(ByteCode& stack, size_t& match_length_minim
match_length_minimum = 0;
break;
case Repetition::Explicit: {
auto repetition_mark_id = m_parser_state.repetition_mark_count++;
ByteCode::transform_bytecode_repetition_min_max(stack, repeat_min.value(), repeat_max, repetition_mark_id, !ungreedy);
auto min_repetition_mark_id = m_parser_state.repetition_mark_count++;
auto max_repetition_mark_id = m_parser_state.repetition_mark_count++;
ByteCode::transform_bytecode_repetition_min_max(stack, repeat_min.value(), repeat_max, min_repetition_mark_id, max_repetition_mark_id, !ungreedy);
match_length_minimum *= repeat_min.value();
break;
}