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:
Norman Maurer 2021-08-26 08:03:35 +02:00 committed by GitHub
parent fc8311cd18
commit 60f8ed01cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -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.

View File

@ -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();