Don't throw if null is given as ByteBuf when adding components. (#11613)

Motivation:

232c669fa4 did add some overflow protection but did not handle null elements in the array the same as before.

Modifications:

- Break the loop if a null element was found
- Add unit test

Result:

Fixes https://github.com/netty/netty/issues/11612
This commit is contained in:
Norman Maurer 2021-08-26 08:20:17 +02:00
parent 839dde1183
commit e21591fa25
2 changed files with 19 additions and 1 deletions

View File

@ -374,7 +374,11 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
int readableBytes = 0;
int capacity = capacity();
for (int i = arrOffset; i < buffers.length; i++) {
readableBytes += buffers[i].readableBytes();
ByteBuf b = buffers[i];
if (b == null) {
break;
}
readableBytes += b.readableBytes();
// Check if we would overflow.
// See https://github.com/netty/netty/issues/10194

View File

@ -1555,6 +1555,20 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
assertTrue(cbuf.release());
}
// See https://github.com/netty/netty/issues/11612
@Test
public void testAddComponentWithNullEntry() {
final ByteBuf buffer = Unpooled.buffer(8).writeZero(8);
final CompositeByteBuf compositeByteBuf = compositeBuffer(Integer.MAX_VALUE);
try {
compositeByteBuf.addComponents(true, new ByteBuf[] { buffer, null });
assertEquals(8, compositeByteBuf.readableBytes());
assertEquals(1, compositeByteBuf.numComponents());
} finally {
compositeByteBuf.release();
}
}
@Test
public void testOverflowWhileAddingComponent() {
int capacity = 1024 * 1024; // 1MB