Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 1c11493

Browse files
committed
Add comments about how component count overflow is prevented
Also add TODOs for flattening composite buffers.
1 parent 1c10770 commit 1c11493

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/main/java/io/netty/buffer/api/CompositeBuf.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ final class CompositeBuf extends RcSupport<Buf, CompositeBuf> implements Buf {
5252
}
5353

5454
private static Buf[] filterExternalBufs(Buf[] bufs) {
55+
// We filter out all zero-capacity buffers because they wouldn't contribute to the composite buffer anyway,
56+
// and also, by ensuring that all constituent buffers contribute to the size of the composite buffer,
57+
// we make sure that the number of composite buffers will never become greater than the number of bytes in
58+
// the composite buffer.
59+
// This restriction guarantees that methods like countComponents, forEachReadable and forEachWritable,
60+
// will never overflow their component counts.
5561
// Allocating a new array unconditionally also prevents external modification of the array.
62+
// TODO if any buffer is itself a composite buffer, then we should unwrap its sub-buffers
5663
return Arrays.stream(bufs).filter(b -> b.capacity() > 0).toArray(Buf[]::new);
5764
}
5865

@@ -625,8 +632,11 @@ void extendWith(Buf extension) {
625632
if (extensionCapacity == 0) {
626633
// Extending by a zero-sized buffer makes no difference. Especially since it's not allowed to change the
627634
// capacity of buffers that are constiuents of composite buffers.
635+
// This also ensures that methods like countComponents, and forEachReadable, do not have to worry about
636+
// overflow in their component counters.
628637
return;
629638
}
639+
// TODO if extension is itself a composite buffer, then we should extend ourselves by all of the sub-buffers
630640

631641
long newSize = capacity() + extensionCapacity;
632642
Allocator.checkSize(newSize);

0 commit comments

Comments
 (0)