diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java index e3fff7057e..c1a3d360fa 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java @@ -525,7 +525,7 @@ public final class ByteBufUtil { try { buffer.writeBytes(src, readerIndex, len); // Use internalNioBuffer(...) to reduce object creation. - decodeString(decoder, buffer.internalNioBuffer(readerIndex, len), dst); + decodeString(decoder, buffer.internalNioBuffer(0, len), dst); } finally { // Release the temporary buffer again. buffer.release(); diff --git a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java index b9c5964ed1..3c913b40ad 100644 --- a/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java +++ b/buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java @@ -106,4 +106,17 @@ public class ByteBufUtilTest { Assert.assertEquals(text, ByteBufUtil.decodeString(buffer, 0, buffer.readableBytes(), charset)); buffer.release(); } + + @Test + public void testToStringDoesNotThrowIndexOutOfBounds() { + CompositeByteBuf buffer = Unpooled.compositeBuffer(); + try { + byte[] bytes = "1234".getBytes(CharsetUtil.UTF_8); + buffer.addComponent(Unpooled.buffer(bytes.length).writeBytes(bytes)); + buffer.addComponent(Unpooled.buffer(bytes.length).writeBytes(bytes)); + Assert.assertEquals("1234", buffer.toString(bytes.length, bytes.length, CharsetUtil.UTF_8)); + } finally { + buffer.release(); + } + } }