8309515: Stale cached data from Matcher.namedGroups() after Matcher.usePattern()

Reviewed-by: rriggs
This commit is contained in:
Raffaello Giulietti 2023-06-07 21:39:53 +00:00
parent ea41907396
commit 90027ff204
2 changed files with 24 additions and 1 deletions
src/java.base/share/classes/java/util/regex
test/jdk/java/util/regex

@ -390,6 +390,7 @@ public final class Matcher implements MatchResult {
if (newPattern == null)
throw new IllegalArgumentException("Pattern cannot be null");
parentPattern = newPattern;
namedGroups = null;
// Reallocate state storage
int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);

@ -23,7 +23,7 @@
/*
* @test
* @bug 8065554
* @bug 8065554 8309515
* @run main NamedGroupsTests
*/
@ -86,6 +86,8 @@ public class NamedGroupsTests {
testMatchResultStartEndGroupBeforeMatchOp();
testMatchResultStartEndGroupAfterMatchOp();
testMatchAfterUsePattern();
}
private static void testMatchResultNoDefault() {
@ -346,4 +348,24 @@ public class NamedGroupsTests {
}
}
private static void testMatchAfterUsePattern() {
Pattern p1 = Pattern.compile("(?<a>...)(?<b>...)");
Matcher m = p1.matcher("foobar");
if (!m.matches()) {
throw new RuntimeException("matches() expected");
}
if (!m.group("a").equals("foo")) {
throw new RuntimeException("\"foo\" expected for group(\"a\")");
}
Pattern p2 = Pattern.compile("(?<b>...)(?<a>...)");
m.usePattern(p2);
if (!m.matches()) {
throw new RuntimeException("matches() expected");
}
if (!m.group("a").equals("bar")) {
throw new RuntimeException("\"bar\" expected for group(\"a\")");
}
}
}