Skip to content

Commit

Permalink
[netty#1976] Fix IndexOutOfBoundsException when calling CompositeByte…
Browse files Browse the repository at this point in the history
…Buf.discardReadComponents()
  • Loading branch information
Norman Maurer committed Nov 9, 2013
1 parent e4f391f commit b00f8c6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -1257,9 +1257,10 @@ public CompositeByteBuf discardReadComponents() {

// 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,4 +790,22 @@ public void testisDirectMultipleBufs() {
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();
}
}

0 comments on commit b00f8c6

Please sign in to comment.