From 6e0d22335d3419d91ae60cb40811a0faa25bed50 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 2 Jun 2020 10:07:50 +0200 Subject: [PATCH] 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 --- .../java/io/netty/buffer/CompositeByteBuf.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index f949a2e78c..c67e0e1e4a 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -267,6 +267,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}. */ @@ -282,9 +289,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; @@ -374,9 +379,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;