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

java.util.regex.Matcher.find(int) throws bogus exception

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs
    • generic
    • generic



      Name: nt126004 Date: 08/29/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)


      The sample code below should print "b@&@&@". Instead, it errors
      out, as shown in the output. Here is the Matcher.find(int)
      method, with the problem area highlighted:

      public boolean find(int start) {
          if ((start < 0) || (start > to))
                              ^^^^^^^^^^
              throw new IndexOutOfBoundsException("Illegal start index");
          reset();
          return find(start, getTextLength());
      }

      The problem is that, because this method was the first one called
      after the matcher was created, the "to" field is still set to zero.
      Either the comparison should be to getTextLength(), or (possibly)
      the "to" value could be set in the reset() method.

      //====================== sample code ===========================

      import java.util.regex.*;

      public class PatternTest
      {
        public static void main(String[] argv)
        {
          Pattern p = Pattern.compile("\\w{punct}+");
          Matcher m = p.matcher("a@&@&@b@&@&@");
          System.out.println(m.find(1) ? m.group() : "not found");
        }
      }

      //======================== output =============================

      $ java PatternTest
      Exception in thread "main" java.lang.IndexOutOfBoundsException:
       Illegal start index
              at java.util.regex.Matcher.find(Matcher.java:425)
              at PatternTest.main(PatternTest.java:9)
      (Review ID: 130863)
      ======================================================================

            Unassigned Unassigned
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: