AdaptiveByteBufAllocator: make sure byteBuf.capacity() not greater th…#16320
Merged
AdaptiveByteBufAllocator: make sure byteBuf.capacity() not greater th…#16320
Conversation
…an byteBuf.maxCapacity() (netty#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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…an byteBuf.maxCapacity() (#16309)
Motivation:
According to the docs of
ByteBuf.maxCapacity():netty/buffer/src/main/java/io/netty/buffer/ByteBuf.java
Lines 265 to 269 in d933624
The
byteBuf.capacity()should always <=byteBuf.maxCapacity().But if we run the following code:
The output:
We can see after reallocation, the
buf.capacity()is greater thanbuf.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 thanbyteBuf.maxCapacity().Co-authored-by: lao
(cherry picked from commit a78ea77)