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

StringIndexOutOfBoundsException in Pattern.compile with CANON_EQ flag

XMLWordPrintable

    • b02
    • generic
    • generic

        FULL PRODUCT VERSION :
        java version "1.8.0_112"
        Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
        Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [version 10.0.14393]

        A DESCRIPTION OF THE PROBLEM :
        Pattern.compile throws StringIndexOutOfBoundsException instead of PatternSyntaxException for the pattern string "[" with canonical equivalence (CANON_EQ) flag set.

        See https://josm.openstreetmap.de/ticket/13870 for a real-life scenario where a pattern is validated during user input.

        As per QUality outreach, please add "josm-found" label to this bug report.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the following code:

        Pattern.compile("[", Pattern.CANON_EQ);

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        PatternSyntaxException
        ACTUAL -
        StringIndexOutOfBoundsException

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        java.lang.StringIndexOutOfBoundsException: String index out of range: 1
        at java.lang.String.codePointAt(String.java:687)
        at java.util.regex.Pattern.normalizeCharClass(Pattern.java:1416)
        at java.util.regex.Pattern.normalize(Pattern.java:1392)
        at java.util.regex.Pattern.compile(Pattern.java:1659)
        at java.util.regex.Pattern.<init>(Pattern.java:1351)
        at java.util.regex.Pattern.compile(Pattern.java:1054)


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.util.regex.Pattern;

        public class PatternBug {
            public static void main(String[] args) throws Exception {
                Pattern.compile("[", Pattern.CANON_EQ);
           }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        catch StringIndexOutOfBoundsException in caller's code

              igerasim Ivan Gerasimov
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated:
                Resolved: