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

Matcher.find() throws StringIndexOutOfBoundsException due to arithmetic overflow

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 17
    • core-libs

      j.u.regex.Pattern.compile(pattern) studies the pattern to determine its minimum/maximum length.
      These values can become invalid due to overflow, which can result in misleading exception thrown during the match operations.

      One example:
      import java.util.regex.*;
      public class T {
          public static void main(String[] args) throws Throwable {
              String pat = "\uD800\uDFFFa{2147483647,}b{2147483647,}";
              Pattern p = Pattern.compile(pat);
              Matcher m = p.matcher("ab");
              System.out.println(m.find());
          }
      }

      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
      at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
      at java.base/java.lang.String.charAt(String.java:711)
      at java.base/java.util.regex.Pattern$StartS.match(Pattern.java:3670)
      at java.base/java.util.regex.Matcher.search(Matcher.java:1729)
      at java.base/java.util.regex.Matcher.find(Matcher.java:746)
      at T.main(T.java:7)

            rgiulietti Raffaello Giulietti
            igerasim Ivan Gerasimov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: