Include more details if we throw an IllegalArgumentException because of overflow (#10330)

Motivation:

We should include as much details as possible when throwing an IllegalArgumentException because of overflow in CompositeByteBuf

Modifications:

Add more details and factor out check into a static method to share code

Result:

Make it more clear why an operations failed
This commit is contained in:
Norman Maurer 2020-06-02 10:07:50 +02:00
parent 32a77394a3
commit 4dfe541167

View File

@ -266,6 +266,13 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
return this;
}
private static void checkForOverflow(int capacity, int readableBytes) {
if (capacity + readableBytes < 0) {
throw new IllegalArgumentException("Can't increase by " + readableBytes + " as capacity(" + capacity + ")" +
" would overflow " + Integer.MAX_VALUE);
}
}
/**
* Precondition is that {@code buffer != null}.
*/
@ -281,9 +288,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
// Check if we would overflow.
// See https://github.com/netty/netty/issues/10194
if (capacity() + readableBytes < 0) {
throw new IllegalArgumentException("Can't increase by " + readableBytes);
}
checkForOverflow(capacity(), readableBytes);
addComp(cIndex, c);
wasAdded = true;
@ -373,9 +378,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
// Check if we would overflow.
// See https://github.com/netty/netty/issues/10194
if (capacity + readableBytes < 0) {
throw new IllegalArgumentException("Can't increase by " + readableBytes);
}
checkForOverflow(capacity, readableBytes);
}
// only set ci after we've shifted so that finally block logic is always correct
int ci = Integer.MAX_VALUE;