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

ByteArrayInputStream.skip(long) can overflow internally

XMLWordPrintable

    • b120
    • x86
    • solaris_nevada
    • Verified

      If you have a ByteArrayInputStream that's not positioned at offset 0, calling skip() with Long.MAX_VALUE does not move the position to the end of the stream and return the number of bytes skipped, as one would expect. This is because skip() checks whether (pos + n > count), which evaluates to false because pos+n overflows and becomes negative.

      To reproduce, run the following code:

      ByteArrayInputStream baos = new ByteArrayInputStream(new byte[1024]);
      System.out.println("Read first byte: " + baos.read());
      System.out.println("skip(): " + baos.skip(Long.MAX_VALUE));
      System.out.println("Read after end of stream: " + baos.read());

      Expected output:

      Read first byte: 0
      skip(): 1023
      Read after end of stream: -1

      Actual output:

      Read first byte: 0
      skip(): 9223372036854775807
      Read after end of stream: 0

            mchung Mandy Chung
            khatlen Knut Anders Hatlen
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: