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

java.lang.String.getChars 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.getChars method does not work with negative srcBegin 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.13):

      "20.12.13 public void getChars(int srcBegin, int srcEnd,
      char dst[], int dstBegin)
      throws NullPointerException,
      IndexOutOfBoundsException

      Characters are copied from this String object into the destination
      character array dst. The first character to be copied is at index
      srcBegin; the last character to be copied is at index srcEnd-1
      (thus the total number of characters to be copied is srcEnd-srcBegin).
      The characters are copied into the subarray of dst starting at index
      dstBegin and ending at index dstbegin+(srcEnd-srcBegin)-1.

      If dst is null, then a NullPointerException is thrown.

      An IndexOutOfBoundsException is thrown if any of the following is true:

            srcBegin is negative
            srcBegin is greater than srcEnd
            srcEnd is greater than the length of this String
            dstBegin is negative
            dstBegin+(srcEnd-srcBegin) is larger than dst.length "

      So method getChars() must throw an IndexOutOfBoundsException if
      srcBegin < 0, but if a private field offset > 0 it may succeed
      (offset is used when this string was created as substring of another
      string).
       
      Here is the minimized test demonstrating the bug:
          

      ----- java_lang_String_getChars.java ---------------------------------------
      class java_lang_String_getChars {

        public static void main(String[] argv) {
            String s1 = "that part of string is hidden and this is not";
            String s2 = s1.substring(34); // get tail of s1
            char[] dst = new char[29];
            try {
              s2.getChars(-34,-5,dst,0); // must throw an exception
              System.out.println("Test failed: unexpected output: "+new String(dst));
            } catch(IndexOutOfBoundsException e) {
              System.out.println("Test passed: expected "+e+" is thrown");
            }
        }

      }

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

      $JAVA java_lang_String_getChars
      Test failed: unexpected output: that part of string is hidden

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

       

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: