From 1e575d73897ebe902789546c50fa5e71d8fba49d Mon Sep 17 00:00:00 2001 From: Nick Hill Date: Mon, 27 May 2019 06:32:08 -0700 Subject: [PATCH] Fix redundant or missing checks and other inconsistencies in ByteBuf impls (#9119) Motivation There are a few minor inconsistencies / redundant operations in the ByteBuf implementations which would be good to fix. Modifications - Unnecessary ByteBuffer.duplicate() performed in CompositeByteBuf.nioBuffer(int,int) - Add missing checkIndex(...) check to ReadOnlyByteBufferBuf.nioBuffer(int,int) - Remove duplicate bounds check in ReadOnlyByteBufferBuf.getBytes(int,byte[],int,int) - Omit redundant bounds check in UnpooledHeapByteBuf.getBytes(int,ByteBuffer) Result More consistency and slightly less overhead --- .../main/java/io/netty/buffer/AbstractByteBuf.java | 11 ++++++----- .../main/java/io/netty/buffer/CompositeByteBuf.java | 2 +- .../java/io/netty/buffer/ReadOnlyByteBufferBuf.java | 6 +----- .../java/io/netty/buffer/UnpooledHeapByteBuf.java | 2 +- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java index 2040fdaf3c..4510f0c7fc 100644 --- a/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java @@ -1335,30 +1335,31 @@ public abstract class AbstractByteBuf extends ByteBuf { checkIndex0(index, fieldLength); } - private static void checkRangeBounds(final int index, final int fieldLength, final int capacity) { + private static void checkRangeBounds(final String indexName, final int index, + final int fieldLength, final int capacity) { if (isOutOfBounds(index, fieldLength, capacity)) { throw new IndexOutOfBoundsException(String.format( - "index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity)); + "%s: %d, length: %d (expected: range(0, %d))", indexName, index, fieldLength, capacity)); } } final void checkIndex0(int index, int fieldLength) { if (checkBounds) { - checkRangeBounds(index, fieldLength, capacity()); + checkRangeBounds("index", index, fieldLength, capacity()); } } protected final void checkSrcIndex(int index, int length, int srcIndex, int srcCapacity) { checkIndex(index, length); if (checkBounds) { - checkRangeBounds(srcIndex, length, srcCapacity); + checkRangeBounds("srcIndex", srcIndex, length, srcCapacity); } } protected final void checkDstIndex(int index, int length, int dstIndex, int dstCapacity) { checkIndex(index, length); if (checkBounds) { - checkRangeBounds(dstIndex, length, dstCapacity); + checkRangeBounds("dstIndex", dstIndex, length, dstCapacity); } } diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index d459890409..f6bbd65de5 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -1629,7 +1629,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements ByteBuffer[] buffers = nioBuffers(index, length); if (buffers.length == 1) { - return buffers[0].duplicate(); + return buffers[0]; } ByteBuffer merged = ByteBuffer.allocate(length).order(order()); diff --git a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java index fcaf4a618c..9dcaf61105 100644 --- a/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java +++ b/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java @@ -194,11 +194,6 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.length); - if (dstIndex < 0 || dstIndex > dst.length - length) { - throw new IndexOutOfBoundsException(String.format( - "dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length)); - } - ByteBuffer tmpBuf = internalNioBuffer(); tmpBuf.clear().position(index).limit(index + length); tmpBuf.get(dst, dstIndex, length); @@ -448,6 +443,7 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf { @Override public ByteBuffer nioBuffer(int index, int length) { + checkIndex(index, length); return (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } diff --git a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java index 4d65522de8..1adb100634 100644 --- a/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/UnpooledHeapByteBuf.java @@ -194,7 +194,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf { @Override public ByteBuf getBytes(int index, ByteBuffer dst) { - checkIndex(index, dst.remaining()); + ensureAccessible(); dst.put(array, index, dst.remaining()); return this; }