question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allocating an unpooled CompositeBuffer of size 1 throws an IllegalArgumentException

See original GitHub issue

Expected behavior

Allocating a new composite buffer with max components equal to 1 via Unpooled.compositeBuffer allocates a new CompositeBuffer instance.

Actual behavior

As of 4.1.32.Final, this method will now throw an IllegalStateException, indicating that the argument must be greater than or equal to two:

java.lang.IllegalArgumentException: maxNumComponents: 1 (expected: >= 2)
        at io.netty.buffer.CompositeByteBuf.<init>(CompositeByteBuf.java:62)
        at io.netty.buffer.CompositeByteBuf.<init>(CompositeByteBuf.java:72)
        at io.netty.buffer.Unpooled.compositeBuffer(Unpooled.java:351)

This is firing in one of our tests. We should fix our code to not allocate composite buffers of this size, but this was a breaking change that we weren’t expecting. This is more of a heads up about the breaking change.

This change was introduced in #8420.

Steps to reproduce

Apply the following patch:

diff --git a/buffer/src/test/java/io/netty/buffer/UnpooledTest.java b/buffer/src/test/java/io/netty/buffer/UnpooledTest.java
index f7fd30ba9..c33570f0f 100644
--- a/buffer/src/test/java/io/netty/buffer/UnpooledTest.java
+++ b/buffer/src/test/java/io/netty/buffer/UnpooledTest.java
@@ -39,6 +39,15 @@ public class UnpooledTest {
     private static final ByteBuf[] EMPTY_BYTE_BUFS = new ByteBuf[0];
     private static final byte[][] EMPTY_BYTES_2D = new byte[0][];

+    @Test
+    public void testCompositeBufferSizeOne() {
+        CompositeByteBuf byteBuf = compositeBuffer(1);
+
+        assertEquals(1, byteBuf.maxNumComponents());
+
+        byteBuf.release();
+    }
+
     @Test
     public void testCompositeWrappedBuffer() {
         ByteBuf header = buffer(12);

and run

$ mvn -pl buffer -Dtest=UnpooledTest#testCompositeBufferSizeOne test

Netty version

4.1.32.Final

JVM version (e.g. java -version)

$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

OS version (e.g. uname -a)

$ uname -a
Darwin nickt.local 18.2.0 Darwin Kernel Version 18.2.0: Fri Oct  5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64 x86_64

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
nicktravcommented, Dec 3, 2018

Sounds good. I’ll try and work up a patch today.

1reaction
normanmaurercommented, Dec 3, 2018

@njhill @nicktrav Yeah I think failing for <= 0 makes sense and we should still allow 1 just to not break people.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unpooled (Netty API Reference (4.1.85.Final))
Creates a new ByteBuf by allocating new space or by wrapping or copying existing byte arrays, byte buffers and a string.
Read more >
io.netty.buffer.Unpooled.java Source code - Java2s.com
Arrays; /** * Creates a new {@link ByteBuf} by allocating new space or by ... MAX_VALUE - length < a.length) { throw new...
Read more >
java/hl845740757/netty-translation/buffer/src/main/java/io/netty ...
Arrays; /** * Creates a new {@link ByteBuf} by allocating new space or by wrapping * or ... to use this operation to...
Read more >
io.netty.buffer.Unpooled - Netty 4.1.72.Final 源码 - 即时通讯网
Unpooled 源码的在线查看和学习,来自 Netty 4.1.72. ... MAX_VALUE - length < a.length) { 442 throw new IllegalArgumentException( 443 "The total length of the ...
Read more >
Netty-ByteBuf | Liuye Notebook
1 Netty ByteBuf ... CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(); ... throw new IllegalArgumentException();
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found