-
Bug
-
Resolution: Fixed
-
P2
-
21, 22
-
b10
-
generic
-
generic
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8314538 | 21.0.1 | Raffaello Giulietti | P2 | Closed | Fixed | b08 |
JDK 21ea31
A DESCRIPTION OF THE PROBLEM :
With the fix for
The correct behavior would be not only consider the start and end of the match but the minimum start and the maximum end of all capturing groups.
REGRESSION : Last worked in version 20.0.2
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the attached program
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It should print
match from 3 to 6: ABC
prefix: 123
suffix: 456
match from 3 to 6: ABC
prefix: 123
suffix: 456
ACTUAL -
match from 3 to 6: ABC
prefix: 123
suffix: 456
match from 3 to 6: ABC
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Range [-3, 0) out of bounds for length 3
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4861)
at java.base/java.lang.String.substring(String.java:2830)
at java.base/java.util.regex.Matcher$ImmutableMatchResult.group(Matcher.java:348)
at CapturingGroups.printResult(CapturingGroups.java:27)
at CapturingGroups.main(CapturingGroups.java:21)
---------- BEGIN SOURCE ----------
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CapturingGroups {
public static void main(String[] args) {
char[] data = "123ABC456".toCharArray();
Matcher m = Pattern.compile("(?<=(\\d{3})).*(?=(\\d{3}))").matcher(CharBuffer.wrap(data));
if(!m.find()) throw new AssertionError();
printResult(m);
MatchResult mr = m.toMatchResult();
Arrays.fill(data, '*');
printResult(mr);
}
private static void printResult(MatchResult mr) {
System.out.println("match from " + mr.start() + " to " + mr.end() + ": " + mr.group());
System.out.println("prefix: " + mr.group(1));
System.out.println("suffix: " + mr.group(2));
}
}
---------- END SOURCE ----------
FREQUENCY : always
- backported by
-
JDK-8314538 MatchResult produces StringIndexOutOfBoundsException for groups outside match
- Closed
- relates to
-
JDK-8314055 Release Note: `java.util.regex.MatchResult` Might Throw `StringIndexOutOfBoundsException` on Regex Patterns Containing Lookaheads and Lookbehinds
- Resolved
-
JDK-8132995 Matcher$ImmutableMatchResult should be optimized to reduce space usage
- Resolved
- links to
-
Commit openjdk/jdk21u/c3eda1c5
-
Commit openjdk/jdk/61c58fdd
-
Review openjdk/jdk21/164
-
Review openjdk/jdk21u/70
-
Review openjdk/jdk/15053