Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in forEachByte on nested composite bytebuf with leak detection #12790

Merged
merged 1 commit into from Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions buffer/src/main/java/io/netty/buffer/WrappedCompositeByteBuf.java
Expand Up @@ -408,6 +408,16 @@ public int forEachByteDesc(int index, int length, ByteProcessor processor) {
return wrapped.forEachByteDesc(index, length, processor);
}

@Override
protected int forEachByteAsc0(int start, int end, ByteProcessor processor) throws Exception {
return wrapped.forEachByteAsc0(start, end, processor);
}

@Override
protected int forEachByteDesc0(int rStart, int rEnd, ByteProcessor processor) throws Exception {
return wrapped.forEachByteDesc0(rStart, rEnd, processor);
}

@Override
public final int hashCode() {
return wrapped.hashCode();
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package io.netty.buffer;

import io.netty.util.ByteProcessor;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.PlatformDependent;
Expand Down Expand Up @@ -1774,4 +1775,36 @@ public void sliceOfCompositeBufferMustThrowISEAfterDiscardBytes() {
composite.release();
}
}

@Test
public void forEachByteOnNestedCompositeByteBufMustSeeEntireFlattenedContents() {
CompositeByteBuf buf = newCompositeBuffer();
buf.addComponent(true, newCompositeBuffer().addComponents(
true,
wrappedBuffer(new byte[] {1, 2, 3}),
wrappedBuffer(new byte[] {4, 5, 6})));
final byte[] arrayAsc = new byte[6];
final byte[] arrayDesc = new byte[6];
buf.forEachByte(new ByteProcessor() {
int index;

@Override
public boolean process(byte value) throws Exception {
arrayAsc[index++] = value;
return true;
}
});
buf.forEachByteDesc(new ByteProcessor() {
int index;

@Override
public boolean process(byte value) throws Exception {
arrayDesc[index++] = value;
return true;
}
});
assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6}, arrayAsc);
assertArrayEquals(new byte[] {6, 5, 4, 3, 2, 1}, arrayDesc);
buf.release();
}
}