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

ByteArrayInputStream.skip(long) can overflow internally

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 6u6
    • Fix Version/s: 7
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      b120
    • CPU:
      x86
    • OS:
      solaris_nevada
    • Verification:
      Verified

      Description

      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

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: