8222684: Better support for patterns
authorigerasim
Wed, 22 May 2019 19:41:59 -0700
changeset 58623 2ffbbd3c662c
parent 58622 1b4ab3ebc51b
child 58624 26867ad686e9
8222684: Better support for patterns 8223163: Better pattern recognition Reviewed-by: ahgross, bchristi, jeff, rhalade, rriggs, smarks
src/java.base/share/classes/java/util/regex/Pattern.java
--- 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);