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

java.lang.String.indexOf method is not compatible to specification

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified



      Name: saf@russia Date: 08/07/96


      This bug was found by St.Petersburg Java SQE team (by Mikhail Gorshenev).

      The java.lang.String.indexOf method does not work with negative fromIndex parameter according
      to the Java language specification.

      The Java Language specification
      (Pre-Release Version 1.0, Draft 5.2 - July 3, 1996)
      says the following (please see item 20.12.24):

      "20.12.24 public int indexOf(int ch, int fromIndex)

      If a character with value ch occurs in the character sequence
      represented by this String object at an index no smaller than
      fromIndex, then the index of the first such occurrence is returned -
      that is, the smallest value k such that:

      (this.charAt(k) == ch) && (k >= fromIndex)

      is true. If no such character occurs in this string at or after position
      fromIndex, then -1 is returned.

      There is no restriction on the value of fromIndex. If it is negative,
      it has the same effect as if it were zero: this entire string may be
      searched. If it is greater than the length of this string, it has
      the same effect as if it were equal to the length of this string: -1 is
      returned."

      Method indexOf must treat negative fromIndex values
      as if it were zero but it doesn't.

      Here is the minimized test demonstrating the bug:
          

      ----- java_lang_String_indexOf.java ---------------------------------------
      class java_lang_String_indexOf {
        public static void main(String argv[]) {
          try {
            int i="some string".indexOf('s',-5);
            if(i==0)
              System.out.println("Test passed: \\"some string\\".indexOf('s',-5) = 0");
            else
              System.out.println("Test failed: \\"some string\\".indexOf('s',-5) = "+i);
          } catch(Throwable e) {
            System.out.println("Test failed: unexpected <"+e+"> is thrown");
          }
        }
      }

      ----- The output of the test: -------------------------

      java java_lang_String_indexOf
      Test failed: unexpected <java.lang.ArrayIndexOutOfBoundsException: -5> is thrown

      -------------------------------------------------------
       

            jrose John Rose
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: