summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2021-04-05 05:49:56 +0430
committerAndreas Kling <kling@serenityos.org>2021-04-05 09:02:06 +0200
commitade97d409444cceecfab812cb497a9559b7bd412 (patch)
treefe04fa86cb214f81faa94929a8cd987543d036e8 /Userland/Libraries
parent1bdc1cf77ee13b6932cc2b9256af597adad5ffed (diff)
downloadserenity-ade97d409444cceecfab812cb497a9559b7bd412.zip
LibRegex: Make sure there are as many group matches as actual matches
Fixes #6131.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js18
-rw-r--r--Userland/Libraries/LibRegex/RegexMatcher.cpp6
2 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js b/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js
index 592314cc5d..6f91ec3900 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js
@@ -101,3 +101,21 @@ test("optionally seen capture group", () => {
expect(res[1]).toBe("mozilla");
expect(res[2]).toBeUndefined();
});
+
+// #6131
+test("capture group with two '?' qualifiers", () => {
+ let res = /()??/.exec("");
+
+ expect(res.length).toBe(2);
+ expect(res[0]).toBe("");
+ expect(res[1]).toBeUndefined();
+});
+
+test("named capture group with two '?' qualifiers", () => {
+ let res = /(?<foo>)??/.exec("");
+
+ expect(res.length).toBe(2);
+ expect(res[0]).toBe("");
+ expect(res[1]).toBeUndefined();
+ expect(res.groups.foo).toBeUndefined();
+});
diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp
index 8bfa00def8..66027e49f4 100644
--- a/Userland/Libraries/LibRegex/RegexMatcher.cpp
+++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp
@@ -254,6 +254,9 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional
MatchOutput output_copy;
if (match_count) {
output_copy.capture_group_matches = output.capture_group_matches;
+ // Make sure there are as many capture matches as there are actual matches.
+ if (output_copy.capture_group_matches.size() < match_count)
+ output_copy.capture_group_matches.resize(match_count);
for (auto& matches : output_copy.capture_group_matches)
matches.resize(m_pattern.parser_result.capture_groups_count + 1);
if (!input.regex_options.has_flag_set(AllFlags::SkipTrimEmptyMatches)) {
@@ -262,6 +265,9 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional
}
output_copy.named_capture_group_matches = output.named_capture_group_matches;
+ // Make sure there are as many capture matches as there are actual matches.
+ if (output_copy.named_capture_group_matches.size() < match_count)
+ output_copy.named_capture_group_matches.resize(match_count);
output_copy.matches = output.matches;
} else {