changeset 52228:b0cef26e900b

8222684: Better support for patterns 8223163: Better pattern recognition Reviewed-by: ahgross, bchristi, jeff, rhalade, rriggs, smarks
author igerasim
date Wed, 22 May 2019 19:41:59 -0700
parents a2ddb1a585cf
children d8e5c7448500
files src/java.base/share/classes/java/util/regex/Pattern.java
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/regex/Pattern.java	Tue Apr 30 16:45:29 2019 -0400
+++ b/src/java.base/share/classes/java/util/regex/Pattern.java	Wed May 22 19:41:59 2019 -0700
@@ -1424,7 +1424,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;
@@ -1963,6 +1967,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;
     }
 
@@ -1973,6 +1981,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;
     }
 
@@ -3407,9 +3419,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);