[#1976] Fix IndexOutOfBoundsException when calling CompositeByteBuf.discardReadComponents()

This commit is contained in:
Norman Maurer 2013-11-09 20:13:24 +01:00
parent e4f391f626
commit b00f8c6390
2 changed files with 21 additions and 2 deletions

View File

@ -1257,9 +1257,10 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
// Update indexes and markers.
Component first = components.get(0);
int offset = first.offset;
updateComponentOffsets(0);
setIndex(readerIndex - first.offset, writerIndex - first.offset);
adjustMarkers(first.offset);
setIndex(readerIndex - offset, writerIndex - offset);
adjustMarkers(offset);
return this;
}

View File

@ -790,4 +790,22 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
buf.addComponent(buffer().writeByte(1));
assertFalse(buf.isDirect());
}
// See https://github.com/netty/netty/issues/1976
@Test
public void testDiscardSomeReadBytes() {
CompositeByteBuf cbuf = freeLater(compositeBuffer());
int len = 8 * 4;
for (int i = 0; i < len; i += 4) {
ByteBuf buf = Unpooled.buffer().writeInt(i);
cbuf.capacity(cbuf.writerIndex()).addComponent(buf).writerIndex(i + 4);
}
cbuf.writeByte(1);
byte[] me = new byte[len];
cbuf.readBytes(me);
cbuf.readByte();
cbuf.discardSomeReadBytes();
}
}