Skip to content

Commit 8a673db

Browse files
author
Norman Maurer
committed
[netty#1644] Fixed IndexOutOfBoundException when calling copy() on a empty CompositeByteBuf
1 parent 5e41698 commit 8a673db

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
4545
private final boolean direct;
4646
private final List<Component> components = new ArrayList<Component>();
4747
private final int maxNumComponents;
48+
private static final ByteBuffer FULL_BYTEBUFFER = (ByteBuffer) ByteBuffer.allocate(1).position(1);
4849

4950
private boolean freed;
5051

@@ -698,6 +699,10 @@ public CompositeByteBuf getBytes(int index, ByteBuffer dst) {
698699
@Override
699700
public CompositeByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
700701
checkDstIndex(index, length, dstIndex, dst.capacity());
702+
if (length == 0) {
703+
return this;
704+
}
705+
701706
int i = toComponentIndex(index);
702707
while (length > 0) {
703708
Component c = components.get(i);
@@ -842,6 +847,9 @@ protected void _setLong(int index, long value) {
842847
@Override
843848
public CompositeByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
844849
checkSrcIndex(index, length, srcIndex, src.length);
850+
if (length == 0) {
851+
return this;
852+
}
845853

846854
int i = toComponentIndex(index);
847855
while (length > 0) {
@@ -864,6 +872,10 @@ public CompositeByteBuf setBytes(int index, ByteBuffer src) {
864872
int length = src.remaining();
865873

866874
checkIndex(index, length);
875+
if (length == 0) {
876+
return this;
877+
}
878+
867879
int i = toComponentIndex(index);
868880
try {
869881
while (length > 0) {
@@ -886,6 +898,9 @@ public CompositeByteBuf setBytes(int index, ByteBuffer src) {
886898
@Override
887899
public CompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
888900
checkSrcIndex(index, length, srcIndex, src.capacity());
901+
if (length == 0) {
902+
return this;
903+
}
889904

890905
int i = toComponentIndex(index);
891906
while (length > 0) {
@@ -905,6 +920,9 @@ public CompositeByteBuf setBytes(int index, ByteBuf src, int srcIndex, int lengt
905920
@Override
906921
public int setBytes(int index, InputStream in, int length) throws IOException {
907922
checkIndex(index, length);
923+
if (length == 0) {
924+
return in.read(EmptyArrays.EMPTY_BYTES);
925+
}
908926

909927
int i = toComponentIndex(index);
910928
int readBytes = 0;
@@ -941,6 +959,9 @@ public int setBytes(int index, InputStream in, int length) throws IOException {
941959
@Override
942960
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
943961
checkIndex(index, length);
962+
if (length == 0) {
963+
return in.read(FULL_BYTEBUFFER);
964+
}
944965

945966
int i = toComponentIndex(index);
946967
int readBytes = 0;
@@ -982,7 +1003,9 @@ public int setBytes(int index, ScatteringByteChannel in, int length) throws IOEx
9821003
public ByteBuf copy(int index, int length) {
9831004
checkIndex(index, length);
9841005
ByteBuf dst = Unpooled.buffer(length);
985-
copyTo(index, length, toComponentIndex(index), dst);
1006+
if (length != 0) {
1007+
copyTo(index, length, toComponentIndex(index), dst);
1008+
}
9861009
return dst;
9871010
}
9881011

buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,18 @@ public void testRemoveLastComponent() {
552552
buf.removeComponent(0);
553553
assertEquals(0, buf.numComponents());
554554
}
555+
556+
@Test
557+
public void testCopyEmpty() {
558+
CompositeByteBuf buf = freeLater(compositeBuffer());
559+
assertEquals(0, buf.numComponents());
560+
assertEquals(0, freeLater(buf.copy()).readableBytes());
561+
}
562+
563+
@Test
564+
public void testDuplicateEmpty() {
565+
CompositeByteBuf buf = freeLater(compositeBuffer());
566+
assertEquals(0, buf.numComponents());
567+
assertEquals(0, freeLater(buf.duplicate()).readableBytes());
568+
}
555569
}

0 commit comments

Comments
 (0)