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

(bf) CharBuffer.subSequence() uses wrong capacity value for new buffer

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2
    • 7
    • 1.4.0, 6u10, 6u14
    • core-libs
    • b57
    • generic, x86, sparc
    • linux, solaris, solaris_8

    Backports

      Description

        FULL PRODUCT VERSION :
        java version "1.6.0_10"
        Java(TM) SE Runtime Environment (build 1.6.0_10-b33)
        Java HotSpot(TM) Server VM (build 11.0-b15, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux tank 2.6.24-23-generic #1 SMP Thu Nov 27 18:13:46 UTC 2008 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        StringCharBuffer.subSequence(start,end) creates a new StringCharBuffer with the wrong capacity value. The current buffer's remaining() value is used as the new buffer's capacity. This causes an IndexOutOfBoundsException when the absolute value of the end index (the new buffer's limit) is greater than remaining().

        Previously (before jdk6u10) the new buffer's capacity was the same as the wrapped string's length. This problem is also present in 6u11.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. javac CharBufTest.java
        2. java CharBufTest

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Result: test
        ACTUAL -
        Exception in thread "main" java.lang.IndexOutOfBoundsException
        at java.nio.StringCharBuffer.subSequence(StringCharBuffer.java:92)
        at CharBufTest.main(CharBufTest.java:9)


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.nio.CharBuffer;

        public class CharBufTest {
            public static void main(String[] args) {
                // Create a 15-char StringCharBuffer via CharBuffer.wrap()
                CharBuffer cb = CharBuffer.wrap("This is a test.");
                // Attempt to select the subsequence "test", starting at position=10
                cb.position(10);
                // Absolute value of end index below (10+4) is greater than remaining (5)
                CharSequence s = cb.subSequence(0, 4); // Fails on jdk6u10 and newer
                System.out.println("Result: " + s);
            }
        }

        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Works OK as long as absolute end index is <= remaining(). Could use slice() first to enforce this.

        Release Regression From : 6u6
        The above release value was the last known release where this
        bug was not reproducible. Since then there has been a regression.

        Attachments

          Issue Links

            Activity

              People

                alanb Alan Bateman
                ndcosta Nelson Dcosta (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: