diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-09-06 02:38:47 +0430 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-09-06 13:51:30 +0430 |
commit | abbe9da25540b7ba992d7c3446cf53d18000cbce (patch) | |
tree | 412f51611bb8b68ccda29e99373cac7d9614e9b4 /Tests | |
parent | f8570bd773731ba8c6fab3ccad1fe8280e352359 (diff) | |
download | serenity-abbe9da25540b7ba992d7c3446cf53d18000cbce.zip |
LibRegex: Make infinite repetitions short-circuit on empty matches
This makes (addmittedly weird) patterns like `(a*)*` work correctly
without going into an infinite fork loop.
Diffstat (limited to 'Tests')
-rw-r--r-- | Tests/LibRegex/Regex.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/Tests/LibRegex/Regex.cpp b/Tests/LibRegex/Regex.cpp index 97c201bb96..6cbb417c3d 100644 --- a/Tests/LibRegex/Regex.cpp +++ b/Tests/LibRegex/Regex.cpp @@ -852,6 +852,21 @@ TEST_CASE(extremely_long_fork_chain) EXPECT_EQ(result.success, true); } +TEST_CASE(theoretically_infinite_loop) +{ + Array patterns { + "(a*)*"sv, // Infinitely matching empty substrings, the outer loop should short-circuit. + "(a*?)*"sv, // Infinitely matching empty substrings, the outer loop should short-circuit. + "(a*)*?"sv, // Should match exactly nothing. + "(?:)*?"sv, // Should not generate an infinite fork loop. + }; + for (auto& pattern : patterns) { + Regex<ECMA262> re(pattern); + auto result = re.match(""); + EXPECT_EQ(result.success, true); + } +} + static auto g_lots_of_a_s = String::repeated('a', 10'000'000); BENCHMARK_CASE(fork_performance) |