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

find() behaves differently between 1.4 and 1.5 for the Matcher

XMLWordPrintable

    • Fix Understood
    • 5.0
    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.5.0_07"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
      Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Linux ghost 2.6.15-23-386 #1 PREEMPT Tue May 23 13:49:40 UTC 2006 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      find() behaves differently between 1.4 and 1.5 for the Matcher when the character sequence is modified in size.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile and run reg15.java that I provided below with a JRE 1.4.2_04 and with a JRE 1.5.0_07.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      using:
      java version "1.4.2_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
      Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)


      a matches: 'a' in: 'bacadav'
      a matches: 'a' in: 'bcadav'
      a matches: 'a' in: 'bcdav'
      a matches: 'a' in: 'bcdv'
      ACTUAL -
      a matches: 'b' in: 'bacadav'
      a matches: 'c' in: 'bcadav'
      a matches: 'd' in: 'bcdav'
      a matches: 'v' in: 'bcdv'
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
              at java.lang.StringBuffer.charAt(StringBuffer.java:163)
              at java.util.regex.Pattern$SingleA.match(Pattern.java:3370)
              at java.util.regex.Pattern$Start.match(Pattern.java:3019)
              at java.util.regex.Matcher.search(Matcher.java:1092)
              at java.util.regex.Matcher.find(Matcher.java:528)
              at reg15.main(reg15.java:10)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
              at java.lang.StringBuffer.charAt(StringBuffer.java:163)
              at java.util.regex.Pattern$SingleA.match(Pattern.java:3370)
              at java.util.regex.Pattern$Start.match(Pattern.java:3019)
              at java.util.regex.Matcher.search(Matcher.java:1092)
              at java.util.regex.Matcher.find(Matcher.java:528)
              at reg15.main(reg15.java:10)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.regex.*;
      import java.lang.StringBuffer;

      public class reg15 {
          public static void main(String[] args) {
          String regex = "a";
          Pattern pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
          StringBuffer sb = new StringBuffer("abacadav");
          Matcher mat = pat.matcher(sb);
                  while (mat.find()){
                      sb.delete(mat.start(),mat.end());
                      System.out.println(regex + " matches: '" + mat.group() + "' in: '" + sb + "'");
                  }
              }

      }

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

            sherman Xueming Shen
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: