John Platts wrote:
Here is the current implementation of slice in java.nio.StringCharBuffer (as found in OpenJDK 7):
44     public CharBuffer slice() {
45         return new StringCharBuffer(str,
46                                     -1,
47                                     0,
48                                     this.remaining(),
49                                     this.remaining(),
50                                     this.position());
51     }

Here is the implementation of slice used for heap-based byte buffers, which is found in java/nio/Heap-X-Buffer.java.template:
 97     public $Type$Buffer slice() {
 98         return new Heap$Type$Buffer$RW$(hb,
 99                                         -1,
100                                         0,
101                                         this.remaining(),
102                                         this.remaining(),
103                                         this.position() + offset);
104     }

It appears that the slice() method fails to return the correct region of the string for CharBuffer objects that wrap a string whenever offset is non-zero. Here is the corrected code, with the correction in red:
44     public CharBuffer slice() {
45         return new StringCharBuffer(str,
46                                     -1,
47                                     0,
48                                     this.remaining(),
49                                     this.remaining(),
50                                     offset + this.position());
51     }

Is this bug limited to OpenJDK 7, or is this bug found in OpenJDK 6, JDK 6, JDK 5, and JDK 1.4? This bug might be affecting code that uses CharBuffers.
Thanks for the bug report. I've created a bug to track it:
7000913: (bf) CharBuffer.wrap, slice, position, slice leads to CharBuffer with incorrect offset

I'm kinda surprised this hasn't been noticed before but it may be that it's rare to slice a buffer that itself was created by slicing a wrapped char sequence. It does highlight a hole in our tests so part of fixing this will adding further tests.

So are you contributing the fix to this? I see a John Platts on the SCA list but I'm not sure if that is you.

As regards versions, then it is likely to exist in all versions.

-Alan

Reply via email to