Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8260866

stack overflow in regex matching quantified alternation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • core-libs
    • None

      I ran into this StackOverflowError while developing https://github.com/openjdk/jdk/pull/1940

      public class RegexStackOverflow {
          public static void main(String[] args) throws Throwable {
              int size = 4096; // 1024 is OK
              java.util.regex.Pattern.compile("(?:X|X)*")
                  .matcher("X".repeat(size) + "!")
                  .matches();
          }
      }

      =>
      Exception in thread "main" java.lang.StackOverflowError
      at java.base/java.util.regex.Pattern.lambda$Single$7(Pattern.java:5711)
      at java.base/java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3966)
      at java.base/java.util.regex.Pattern$Branch.match(Pattern.java:4746)
      at java.base/java.util.regex.Pattern$GroupHead.match(Pattern.java:4801)
      at java.base/java.util.regex.Pattern$Loop.match(Pattern.java:4910)
      at java.base/java.util.regex.Pattern$GroupTail.match(Pattern.java:4832)
      at java.base/java.util.regex.Pattern$BranchConn.match(Pattern.java:4710)
      at java.base/java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3967)

      the part of the stack around main is not shown, which is not great. If you're going to leave out part of a stack trace for stack overflow, leave out the stuff in the middle!

            smarks Stuart Marks
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: