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

Pattern with a repeating non-capturing group causes StackOverflowError

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      $ lsb_release -a
      No LSB modules are available.
      Distributor ID: Ubuntu
      Description: Ubuntu 17.10
      Release: 17.10
      Codename: artful

      A DESCRIPTION OF THE PROBLEM :
      A simple pattern with a non-capturing group causes StackOverflowError on small-ish input of 1600 integers.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Source code attached.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The operation should complete successfully based on length of input to heap memory and not sensitive to stack memory limit.
      ACTUAL -
      The operation caused a StackOverflowError.

      ---------- BEGIN SOURCE ----------
      import java.io.IOException;
      import java.nio.charset.StandardCharsets;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.util.List;
      import java.util.concurrent.ThreadLocalRandom;
      import java.util.regex.Pattern;

      public class Test {
          public static void main(String[] args) {
              Pattern pattern = Pattern.compile("^[1-9][0-9]*(?:, [1-9][0-9]*)*$");

              for (int n = 100; n > 0; n *= 2) {
                  test(pattern, n);
              }
          }

          private static void test(Pattern pattern, int n) {
              String line = generate(n);

              System.out.print("Testing line of " + n + " numbers...");
              boolean found = pattern.matcher(line).find();
              System.out.println(found ? "matched." : "not matched.");
          }

          private static String generate(int n) {
              StringBuilder buf = new StringBuilder();
              buf.append(ThreadLocalRandom.current().nextInt(1, 1000));
              for (n--; n > 0; n--) {
                  buf.append(", ");
                  buf.append(ThreadLocalRandom.current().nextInt(1, 1000));
              }
              return buf.toString();
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: