[#529] ByteBuf.ensureWritableBytes() can trigger IllegalArgumentException
- Remove redundant boundary check in ensureWritableBytes() - Ensure calculateNewCapacity() never returns the value that exceeds maxCapacity
This commit is contained in:
parent
aef7a14852
commit
505e767a09
@ -180,27 +180,25 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (minWritableBytes > maxCapacity - writerIndex) {
|
if (minWritableBytes < 0) {
|
||||||
throw new IllegalArgumentException(String.format(
|
throw new IllegalArgumentException(String.format(
|
||||||
"minWritableBytes(%d) + writerIndex(%d) > maxCapacity(%d)",
|
"minWritableBytes: %d (expected: 0+)", minWritableBytes));
|
||||||
minWritableBytes, writerIndex, maxCapacity));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int minNewCapacity = writerIndex + minWritableBytes;
|
if (minWritableBytes > maxCapacity - writerIndex) {
|
||||||
|
|
||||||
if (minNewCapacity > maxCapacity) {
|
|
||||||
throw new IllegalArgumentException(String.format(
|
throw new IllegalArgumentException(String.format(
|
||||||
"minWritableBytes: %d (exceeds maxCapacity(%d))", minWritableBytes, maxCapacity));
|
"minWritableBytes: %d (exceeds maxCapacity(%d))", minWritableBytes, maxCapacity));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize the current capacity to the power of 2.
|
// Normalize the current capacity to the power of 2.
|
||||||
int newCapacity = calculateNewCapacity(minNewCapacity);
|
int newCapacity = calculateNewCapacity(writerIndex + minWritableBytes);
|
||||||
|
|
||||||
// Adjust to the new capacity.
|
// Adjust to the new capacity.
|
||||||
capacity(newCapacity);
|
capacity(newCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int calculateNewCapacity(int minNewCapacity) {
|
private int calculateNewCapacity(int minNewCapacity) {
|
||||||
|
final int maxCapacity = this.maxCapacity;
|
||||||
final int threshold = 1048576 * 4; // 4 MiB page
|
final int threshold = 1048576 * 4; // 4 MiB page
|
||||||
|
|
||||||
if (minNewCapacity == threshold) {
|
if (minNewCapacity == threshold) {
|
||||||
@ -223,7 +221,8 @@ public abstract class AbstractByteBuf implements ByteBuf {
|
|||||||
while (newCapacity < minNewCapacity) {
|
while (newCapacity < minNewCapacity) {
|
||||||
newCapacity <<= 1;
|
newCapacity <<= 1;
|
||||||
}
|
}
|
||||||
return newCapacity;
|
|
||||||
|
return Math.min(newCapacity, maxCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user