Skip to content

Auto-port 5.0: AdaptiveByteBufAllocator: make sure byteBuf.capacity() not greater than byteBuf.maxCapacity()#16318

Merged
normanmaurer merged 4 commits into5.0from
auto-port-pr-16309-to-5.0
Feb 23, 2026
Merged

Auto-port 5.0: AdaptiveByteBufAllocator: make sure byteBuf.capacity() not greater than byteBuf.maxCapacity()#16318
normanmaurer merged 4 commits into5.0from
auto-port-pr-16309-to-5.0

Conversation

@netty-project-bot
Copy link
Copy Markdown
Contributor

Auto-port of #16309 to 5.0
Cherry-picked commit: a78ea77


Motivation:

According to the docs of ByteBuf.maxCapacity():

/**
* Returns the maximum allowed capacity of this buffer. This value provides an upper
* bound on {@link #capacity()}.
*/
public abstract int maxCapacity();

The byteBuf.capacity() should always <= byteBuf.maxCapacity().

But if we run the following code:

public static void main(String[] args) {
    int maxSize = 100000;
    AdaptiveByteBufAllocator alloc = new AdaptiveByteBufAllocator();
    ByteBuf buf = alloc.newDirectBuffer(maxSize, maxSize);
    System.out.println("Before reallocation: buf.capacity(): " + buf.capacity());
    System.out.println("Before reallocation: buf.maxCapacity(): " + buf.maxCapacity());
    buf = buf.capacity(maxSize + 1); // Should not be allowed.
    System.out.println("After reallocation: buf.capacity(): " + buf.capacity());
    System.out.println("After reallocation: buf.maxCapacity(): " + buf.maxCapacity());
    //assert buf.capacity() <= buf.maxCapacity();
}

The output:

Before reallocation: buf.capacity(): 100000
Before reallocation: buf.maxCapacity(): 100000
After reallocation: buf.capacity(): 100001
After reallocation: buf.maxCapacity(): 100000

We can see after reallocation, the buf.capacity() is greater than buf.maxCapacity(), which should not happen.

Modification:

Adjust the check in AdaptivePoolingAllocator.capacity(int newCapacity), to make sure the constrain hold.

Result:

Make sure byteBuf.capacity() not greater than byteBuf.maxCapacity().

…an byteBuf.maxCapacity() (#16309)

Motivation:

According to the docs of `ByteBuf.maxCapacity()`:

https://github.com/netty/netty/blob/d9336245a2fe3dfec4d0d1e489135bbf7ed03160/buffer/src/main/java/io/netty/buffer/ByteBuf.java#L265-L269

The `byteBuf.capacity()` should always <= `byteBuf.maxCapacity()`.

But if we run the following code:
```
public static void main(String[] args) {
    int maxSize = 100000;
    AdaptiveByteBufAllocator alloc = new AdaptiveByteBufAllocator();
    ByteBuf buf = alloc.newDirectBuffer(maxSize, maxSize);
    System.out.println("Before reallocation: buf.capacity(): " + buf.capacity());
    System.out.println("Before reallocation: buf.maxCapacity(): " + buf.maxCapacity());
    buf = buf.capacity(maxSize + 1); // Should not be allowed.
    System.out.println("After reallocation: buf.capacity(): " + buf.capacity());
    System.out.println("After reallocation: buf.maxCapacity(): " + buf.maxCapacity());
    //assert buf.capacity() <= buf.maxCapacity();
}
```
The output:

> Before reallocation: buf.capacity(): 100000
> Before reallocation: buf.maxCapacity(): 100000
> After reallocation: buf.capacity(): 100001
> After reallocation: buf.maxCapacity(): 100000

We can see after reallocation, the `buf.capacity()` is greater than
`buf.maxCapacity()`, which should not happen.

Modification:

Adjust the check in `AdaptivePoolingAllocator.capacity(int
newCapacity)`, to make sure the constrain hold.

Result:

Make sure `byteBuf.capacity()` not greater than `byteBuf.maxCapacity()`.

---------

Co-authored-by: lao <none>
(cherry picked from commit a78ea77)
@normanmaurer normanmaurer merged commit 217a8d9 into 5.0 Feb 23, 2026
17 of 23 checks passed
@normanmaurer normanmaurer deleted the auto-port-pr-16309-to-5.0 branch February 23, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants