Make CompositeByteBuf throw IllegalStateException when components are missing (#11100)

Motivation:
Components in a composite buffer can "go missing" if the composite is a slice of another composite and the parent has changed its layout.

Modification:
Where we would previously have thrown a NullPointerException, we now have a null-check for the component, and we instead throw an IllegalStateException with a more descriptive message.

Result:
It's now a bit easier to understand what is going on in these situations.

Fixes #10908
This commit is contained in:
Chris Vest 2021-03-18 17:51:02 +01:00
parent 5deaf94bfe
commit 654a54bbad
2 changed files with 24 additions and 0 deletions

View File

@ -1600,6 +1600,10 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf implements
for (int low = 0, high = componentCount; low <= high;) {
int mid = low + high >>> 1;
Component c = components[mid];
if (c == null) {
throw new IllegalStateException("No component found for offset. " +
"Composite buffer layout might be outdated, e.g. from a discardReadBytes call.");
}
if (offset >= c.endOffset) {
low = mid + 1;
} else if (offset < c.offset) {

View File

@ -1604,4 +1604,24 @@ public abstract class AbstractCompositeByteBufTest extends AbstractByteBufTest {
compositeByteBuf.release();
}
}
@Test
public void sliceOfCompositeBufferMustThrowISEAfterDiscardBytes() {
CompositeByteBuf composite = compositeBuffer();
composite.addComponent(true, buffer(8).writeZero(8));
ByteBuf slice = composite.retainedSlice();
composite.skipBytes(slice.readableBytes());
composite.discardSomeReadBytes();
try {
slice.readByte();
fail("Expected readByte of discarded slice to throw.");
} catch (IllegalStateException ignore) {
// Good.
} finally {
slice.release();
composite.release();
}
}
}