LibRegex: Treat backwards jumps to IP 0 as normal backwards jumps too

This shows up in something like /\d+|x/, where the `+` ends up jumping
to the start of its own alternative.
This commit is contained in:
Ali Mohammad Pur 2023-08-16 19:48:50 +03:30 committed by Ali Mohammad Pur
parent 045880e6c7
commit 4d27257c45
Notes: sideshowbarker 2024-07-17 06:20:50 +09:00
2 changed files with 3 additions and 1 deletions

View File

@ -1051,6 +1051,8 @@ TEST_CASE(optimizer_alternation)
Tuple { "ab|ac|ad|bc"sv, "bc"sv, 2u },
// Should not crash on backwards jumps introduced by '.*'.
Tuple { "\\bDroid\\b.*Build|XT912|XT928|XT926|XT915|XT919|XT925|XT1021|\\bMoto E\\b|XT1068|XT1092|XT1052"sv, "XT1068"sv, 6u },
// Backwards jumps to IP 0 are normal jumps too.
Tuple { "^(\\d+|x)"sv, "42"sv, 2u },
};
for (auto& test : tests) {

View File

@ -1150,7 +1150,7 @@ void Optimizer::append_alternation(ByteCode& target, Span<ByteCode> alternatives
VERIFY(node->has_metadata());
QualifiedIP ip = node->metadata_value().first();
auto intended_jump_ip = ip.instruction_position + jump_offset + opcode.size();
if (jump_offset < 0 && intended_jump_ip > 0) {
if (jump_offset < 0) {
VERIFY(has_any_backwards_jump);
// We should've already seen this instruction, so we can just patch it in.
auto& ip_mapping = ip_mapping_for_alternative(ip.alternative_index);