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

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

XMLWordPrintable

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified



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


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

      The java.lang.String.getBytes(int srcBegin,int srcEnd, byte dst[], int dstBegin) method does not work according to the
      Java language specification when srcEnd > string length and srcBegin=srcEnd

      The Java Language specification (Version 1.0, August 1, 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 "


      Here is the minimized test demonstrating the bug:

      ----- java_lang_String_getBytes.java ---------------------------------------
      class java_lang_String_getBytes {
        public static void main(String argv[]) {
            byte data[]=new byte[5];
            try {
              String s = "test";
              s.getBytes(10,10,data,0);
              System.out.println("Test failed: exception not thrown");
            }
            catch (IndexOutOfBoundsException e) {
              System.out.println("Test passed: " + e);
            }
        }
      }

      ----- The output of the test: -------------------------
      Test failed: exception not thrown
      -------------------------------------------------------


      Workaround:
      None
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: