From 451e91d1429c08ce8f92bb8ce0b9ad8c28afb904 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Mon, 9 Sep 2013 20:29:30 +0200 Subject: [PATCH] [#1821] Fix IndexOutOfBoundsException which was thrown if the last component was removed but other components was left --- .../main/java/io/netty/buffer/CompositeByteBuf.java | 5 +++-- .../io/netty/buffer/AbstractCompositeByteBufTest.java | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index bb21bc7e44..50aa120a09 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -323,7 +323,8 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { } private void updateComponentOffsets(int cIndex) { - if (components.isEmpty()) { + int size = components.size(); + if (size <= cIndex) { return; } @@ -334,7 +335,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { cIndex ++; } - for (int i = cIndex; i < components.size(); i ++) { + for (int i = cIndex; i < size; i ++) { Component prev = components.get(i - 1); Component cur = components.get(i); cur.offset = prev.endOffset; diff --git a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java index 1e76d6dc1e..350ef23ce0 100644 --- a/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java @@ -567,6 +567,16 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest { assertEquals(0, freeLater(buf.duplicate()).readableBytes()); } + @Test + public void testRemoveLastComponentWithOthersLeft() { + CompositeByteBuf buf = freeLater(compositeBuffer()); + buf.addComponent(wrappedBuffer(new byte[]{1, 2})); + buf.addComponent(wrappedBuffer(new byte[]{1, 2})); + assertEquals(2, buf.numComponents()); + buf.removeComponent(1); + assertEquals(1, buf.numComponents()); + } + @Override @Test public void testInternalNioBuffer() {