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
This commit is contained in:
Boris Unckel 2021-04-22 14:20:44 +02:00 committed by GitHub
parent c3416d8ad2
commit d5fabe78a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -15,6 +15,8 @@
*/ */
package io.netty.buffer; package io.netty.buffer;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import io.netty.util.ReferenceCounted; import io.netty.util.ReferenceCounted;
import io.netty.util.internal.ObjectUtil; import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
@ -108,7 +110,7 @@ public class ByteBufInputStream extends InputStream implements DataInput {
if (releaseOnClose) { if (releaseOnClose) {
buffer.release(); buffer.release();
} }
throw new IllegalArgumentException("length: " + length); checkPositiveOrZero(length, "length");
} }
if (length > buffer.readableBytes()) { if (length > buffer.readableBytes()) {
if (releaseOnClose) { if (releaseOnClose) {

View File

@ -250,9 +250,13 @@ public final class ByteBufUtil {
* {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]} * {@code a[aStartIndex : aStartIndex + length] == b[bStartIndex : bStartIndex + length]}
*/ */
public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length) { public static boolean equals(ByteBuf a, int aStartIndex, ByteBuf b, int bStartIndex, int length) {
if (aStartIndex < 0 || bStartIndex < 0 || length < 0) { checkNotNull(a, "a");
throw new IllegalArgumentException("All indexes and lengths must be non-negative"); checkNotNull(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) { if (a.writerIndex() - length < aStartIndex || b.writerIndex() - length < bStartIndex) {
return false; return false;
} }