From 73bdca9442e3abff1344da7be72dbdc5b4ef0a40 Mon Sep 17 00:00:00 2001 From: Boris Unckel Date: Thu, 22 Apr 2021 14:20:44 +0200 Subject: [PATCH] Utilize i.n.u.internal.ObjectUtil to assert Preconditions (buffer) (#11170) (#11182) Motivation: NullChecks resulting in a NullPointerException or IllegalArgumentException, numeric ranges (>0, >=0) checks, not empty strings/arrays checks must never be anonymous but with the parameter or variable name which is checked. They must be specific and should not be done with an "OR-Logic" (if a == null || b == null) throw new NullPointerEx. Modifications: * import static relevant checks * Replace manual checks with ObjectUtil methods Result: All checks needed are done with ObjectUtil, some exception texts are improved. Fixes #11170 --- .../main/java/io/netty/buffer/ByteBufInputStream.java | 3 ++- buffer/src/main/java/io/netty/buffer/ByteBufUtil.java | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java b/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java index 146c72963a..c23fb9d91c 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java @@ -16,6 +16,7 @@ package io.netty.buffer; import static java.util.Objects.requireNonNull; +import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero; import io.netty.util.ReferenceCounted; import io.netty.util.internal.StringUtil; @@ -111,7 +112,7 @@ public class ByteBufInputStream extends InputStream implements DataInput { if (releaseOnClose) { buffer.release(); } - throw new IllegalArgumentException("length: " + length); + checkPositiveOrZero(length, "length"); } if (length > buffer.readableBytes()) { if (releaseOnClose) { diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java index 2e1fe5a711..6f46c7f09e 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java @@ -249,9 +249,13 @@ public final class ByteBufUtil { * {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]} */ public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length) { - if (aStartIndex < 0 || bStartIndex < 0 || length < 0) { - throw new IllegalArgumentException("All indexes and lengths must be non-negative"); - } + requireNonNull(a, "a"); + requireNonNull(b, "b"); + // All indexes and lengths must be non-negative + checkPositiveOrZero(aStartIndex, "aStartIndex"); + checkPositiveOrZero(bStartIndex, "bStartIndex"); + checkPositiveOrZero(length, "length"); + if (a.writerIndex() - length < aStartIndex || b.writerIndex() - length < bStartIndex) { return false; }