8311939: Excessive allocation of Matcher.groups array

Reviewed-by: rriggs, igraves
This commit is contained in:
Cristian Vat 2023-08-17 11:27:39 +00:00 committed by Raffaello Giulietti
parent ed585d16b9
commit 32efd23c5d
3 changed files with 66 additions and 4 deletions

View file

@ -247,8 +247,7 @@ public final class Matcher implements MatchResult {
this.text = text;
// Allocate state storage
int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
groups = new int[parentGroupCount * 2];
groups = new int[parent.capturingGroupCount * 2];
locals = new int[parent.localCount];
localsPos = new IntHashSet[parent.localTCNCount];
@ -422,8 +421,7 @@ public final class Matcher implements MatchResult {
namedGroups = null;
// Reallocate state storage
int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
groups = new int[parentGroupCount * 2];
groups = new int[newPattern.capturingGroupCount * 2];
locals = new int[newPattern.localCount];
for (int i = 0; i < groups.length; i++)
groups[i] = -1;

View file

@ -5187,6 +5187,12 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
groupIndex = groupCount + groupCount;
}
boolean match(Matcher matcher, int i, CharSequence seq) {
// reference to not existing group must never match
// group does not exist if matcher didn't allocate space for it
if (groupIndex >= matcher.groups.length) {
return false;
}
int j = matcher.groups[groupIndex];
int k = matcher.groups[groupIndex+1];
@ -5223,6 +5229,12 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
this.doUnicodeCase = doUnicodeCase;
}
boolean match(Matcher matcher, int i, CharSequence seq) {
// reference to not existing group must never match
// group does not exist if matcher didn't allocate space for it
if (groupIndex >= matcher.groups.length) {
return false;
}
int j = matcher.groups[groupIndex];
int k = matcher.groups[groupIndex+1];