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

Matcher.find() throws StringIndexOutOfBoundsException due to arithmetic overflow

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      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)

        Attachments

          Activity

            People

            Assignee:
            igerasim Ivan Gerasimov
            Reporter:
            igerasim Ivan Gerasimov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: