8222684: Better support for patterns
8223163: Better pattern recognition
Reviewed-by: ahgross, bchristi, jeff, rhalade, rriggs, smarks
--- a/src/java.base/share/classes/java/util/regex/Pattern.java Wed May 22 13:31:42 2019 -0400
+++ b/src/java.base/share/classes/java/util/regex/Pattern.java Wed May 22 19:41:59 2019 -0700
@@ -1427,7 +1427,11 @@
localTCNCount = 0;
if (!pattern.isEmpty()) {
- compile();
+ try {
+ compile();
+ } catch (StackOverflowError soe) {
+ throw error("Stack overflow during pattern compilation");
+ }
} else {
root = new Start(lastAccept);
matchRoot = lastAccept;
@@ -1965,6 +1969,10 @@
int ch = temp[cursor++];
while (ch != 0 && !isLineSeparator(ch))
ch = temp[cursor++];
+ if (ch == 0 && cursor > patternLength) {
+ cursor = patternLength;
+ ch = temp[cursor++];
+ }
return ch;
}
@@ -1975,6 +1983,10 @@
int ch = temp[++cursor];
while (ch != 0 && !isLineSeparator(ch))
ch = temp[++cursor];
+ if (ch == 0 && cursor > patternLength) {
+ cursor = patternLength;
+ ch = temp[cursor];
+ }
return ch;
}
@@ -3415,9 +3427,10 @@
private int N() {
if (read() == '{') {
int i = cursor;
- while (cursor < patternLength && read() != '}') {}
- if (cursor > patternLength)
- throw error("Unclosed character name escape sequence");
+ while (read() != '}') {
+ if (cursor >= patternLength)
+ throw error("Unclosed character name escape sequence");
+ }
String name = new String(temp, i, cursor - i - 1);
try {
return Character.codePointOf(name);