From 9eccd4c56e09fb5b7a08e4288d174f5a27c75695 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 21 Jan 2022 14:50:30 +0330 Subject: [PATCH] LibRegex: Allow the pattern to match the zero-length end of the string ...only if Multiline is not enabled. Fixes #11940. --- Tests/LibRegex/Regex.cpp | 1 + Userland/Libraries/LibRegex/RegexMatcher.cpp | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Tests/LibRegex/Regex.cpp b/Tests/LibRegex/Regex.cpp index d054f467e5..c9bb6e1622 100644 --- a/Tests/LibRegex/Regex.cpp +++ b/Tests/LibRegex/Regex.cpp @@ -683,6 +683,7 @@ TEST_CASE(ECMA262_match) { "[\\01]"sv, "\1"sv, true, ECMAScriptFlags::BrowserExtended }, { "(\0|a)"sv, "a"sv, true }, // #9686, Should allow null bytes in pattern { "(.*?)a(?!(a+)b\\2c)\\2(.*)"sv, "baaabaac"sv, true }, // #6042, Groups inside lookarounds may be referenced outside, but their contents appear empty if the pattern in the lookaround fails. + { "a|$"sv, "x"sv, true, (ECMAScriptFlags)regex::AllFlags::Global }, // #11940, Global (not the 'g' flag) regexps should attempt to match the zero-length end of the string too. }; // clang-format on diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp index 26ae07d886..18da4dd370 100644 --- a/Userland/Libraries/LibRegex/RegexMatcher.cpp +++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp @@ -224,7 +224,10 @@ RegexResult Matcher::match(Vector const& views, Optiona } } - for (; view_index < view_length; ++view_index) { + for (; view_index <= view_length; ++view_index) { + if (view_index == view_length && input.regex_options.has_flag_set(AllFlags::Multiline)) + break; + auto& match_length_minimum = m_pattern->parser_result.match_length_minimum; // FIXME: More performant would be to know the remaining minimum string // length needed to match from the current position onwards within