Correctly respect array offset when check for overflow (#11614)
Motivation:
232c669fa4
did add overflow protection but did miss to take the array offset into account and so could report false-positives
Modifications:
- Correctly take offset into account when check for overflow.
- Add unit tests
Result:
Correctly take offset into account when overflow check is performed
This commit is contained in:
parent
fc8311cd18
commit
60f8ed01cd
|
@ -374,7 +374,7 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
|
|||
|
||||
int readableBytes = 0;
|
||||
int capacity = capacity();
|
||||
for (int i = 0; i < buffers.length; i++) {
|
||||
for (int i = arrOffset; i < buffers.length; i++) {
|
||||
readableBytes += buffers[i].readableBytes();
|
||||
|
||||
// Check if we would overflow.
|
||||
|
|
|
@ -1642,6 +1642,47 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverflowWhileUseConstructorWithOffset() {
|
||||
int capacity = 1024 * 1024; // 1MB
|
||||
final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
|
||||
final List<ByteBuf> buffers = new ArrayList<ByteBuf>();
|
||||
for (long i = 0; i <= Integer.MAX_VALUE; i += capacity) {
|
||||
buffers.add(buffer.duplicate());
|
||||
}
|
||||
// Add one more
|
||||
buffers.add(buffer.duplicate());
|
||||
|
||||
try {
|
||||
assertThrows(IllegalArgumentException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
ByteBuf[] bufferArray = buffers.toArray(new ByteBuf[0]);
|
||||
new CompositeByteBuf(ALLOC, false, Integer.MAX_VALUE, bufferArray, 0);
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
buffer.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotOverflowWhileUseConstructorWithOffset() {
|
||||
int capacity = 1024 * 1024; // 1MB
|
||||
final ByteBuf buffer = Unpooled.buffer(capacity).writeZero(capacity);
|
||||
final List<ByteBuf> buffers = new ArrayList<ByteBuf>();
|
||||
for (long i = 0; i <= Integer.MAX_VALUE; i += capacity) {
|
||||
buffers.add(buffer.duplicate());
|
||||
}
|
||||
// Add one more
|
||||
buffers.add(buffer.duplicate());
|
||||
|
||||
ByteBuf[] bufferArray = buffers.toArray(new ByteBuf[0]);
|
||||
CompositeByteBuf compositeByteBuf =
|
||||
new CompositeByteBuf(ALLOC, false, Integer.MAX_VALUE, bufferArray, bufferArray.length - 1);
|
||||
compositeByteBuf.release();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sliceOfCompositeBufferMustThrowISEAfterDiscardBytes() {
|
||||
CompositeByteBuf composite = compositeBuffer();
|
||||
|
|
Loading…
Reference in New Issue