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

java.lang.String.getBytes method works wrong

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.getBytes 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.14):

      "20.12.14 public void getBytes(int srcBegin, int srcEnd,
      byte dst[], int dstBegin)
      throws NullPointerException,
      IndexOutOfBoundsException

      Characters are copied from this String object into the destination byte array
      dst. Each byte receives only the eight low-order bits of the corresponding
      character. The eight high-order bits of each character are not copied and do not
      participate in the transfer in any way. 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,
      converted to bytes, 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 getBytes() 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_getBytes.java ---------------------------------------
      class java_lang_String_getBytes {

        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
            byte[] dst = new byte[29];
            try {
              s2.getBytes(-34,-5,dst,0); // must throw an exception
              System.out.println("Test failed: unexpected output: "+new String(dst,0));
            } catch(IndexOutOfBoundsException e) {
              System.out.println("Test passed: expected "+e+" is thrown");
            }
        }

      }

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

      $JAVA java_lang_String_getBytes
      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: