GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Test and fix the issue with CompositeByteBuf.component() indexes · grpc/grpc-java@30d014e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • netty/src
    • main/java/io/grpc/netty
      • NettyAdaptiveCumulator.java
    • test/java/io/grpc/netty
      • NettyAdaptiveCumulatorTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎netty/src/main/java/io/grpc/netty/NettyAdaptiveCumulator.java‎

Copy file name to clipboardExpand all lines: netty/src/main/java/io/grpc/netty/NettyAdaptiveCumulator.java
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ static void mergeWithCompositeTail(
154154
// Ideal case: the tail isn't shared, and can be expanded to the required capacity.
155155
// Take ownership of the tail.
156156
newTail = tail.retain();
157+
158+
// TODO(sergiitk): remove when netty issue resolved.
159+
// Correct the indexes.
160+
ByteBuf sliceDuplicate = composite.internalComponent(tailComponentIndex).duplicate();
161+
newTail.setIndex(sliceDuplicate.readerIndex(), sliceDuplicate.writerIndex());
162+
157163
/*
158164
* The tail is a readable non-composite buffer, so writeBytes() handles everything for us.
159165
*
Collapse file

‎netty/src/test/java/io/grpc/netty/NettyAdaptiveCumulatorTest.java‎

Copy file name to clipboardExpand all lines: netty/src/test/java/io/grpc/netty/NettyAdaptiveCumulatorTest.java
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,57 @@ public CompositeByteBuf addFlattenedComponents(boolean increaseWriterIndex,
585585
}
586586
}
587587
}
588+
589+
/**
590+
* Miscellaneous tests for {@link NettyAdaptiveCumulator#mergeWithCompositeTail} that don't
591+
* fit into {@link MergeWithCompositeTailTests}, and require custom-crafted scenarios.
592+
*/
593+
@RunWith(JUnit4.class)
594+
public static class MergeWithCompositeTailMiscTests {
595+
private final ByteBufAllocator alloc = new PooledByteBufAllocator();
596+
597+
/**
598+
* Test the issue with {@link CompositeByteBuf#component(int)} returning a ByteBuf with
599+
* the indexes out-of-sync with {@code CompositeByteBuf.Component} offsets.
600+
*/
601+
@Test
602+
public void mergeWithCompositeTail_outOfSyncComposite() {
603+
NettyAdaptiveCumulator cumulator = new NettyAdaptiveCumulator(1024);
604+
605+
// Create underlying buffer spacious enough for the test data.
606+
ByteBuf buf = alloc.buffer(32).writeBytes("---01234".getBytes(US_ASCII));
607+
608+
// Start with a regular cumulation and add the buf as the only component.
609+
CompositeByteBuf composite1 = alloc.compositeBuffer(8).addFlattenedComponents(true, buf);
610+
// Read composite1 buf to the beginning of the numbers.
611+
assertThat(composite1.readCharSequence(3, US_ASCII).toString()).isEqualTo("---");
612+
613+
// Wrap composite1 into another cumulation. This is similar to
614+
// what NettyAdaptiveCumulator.cumulate() does in the case the cumulation has refCnt != 1.
615+
CompositeByteBuf composite2 =
616+
alloc.compositeBuffer(8).addFlattenedComponents(true, composite1);
617+
assertThat(composite2.toString(US_ASCII)).isEqualTo("01234");
618+
619+
// The previous operation does not adjust the read indexes of the underlying buffers,
620+
// only the internal Component offsets. When the cumulator attempts to append the input to
621+
// the tail buffer, it extracts it from the cumulation, writes to it, and then adds it back.
622+
// Because the readerIndex on the tail buffer is not adjusted during the read operation
623+
// on the CompositeByteBuf, adding the tail back results in the discarded bytes of the tail
624+
// to be added back to the cumulator as if they were never read.
625+
//
626+
// If the reader index of the tail is not manually corrected, the resulting
627+
// cumulation will contain the discarded part of the tail: "---".
628+
// If it's corrected, it will only contain the numbers.
629+
CompositeByteBuf cumulation = (CompositeByteBuf) cumulator.cumulate(alloc, composite2,
630+
ByteBufUtil.writeAscii(alloc, "56789"));
631+
assertThat(cumulation.toString(US_ASCII)).isEqualTo("0123456789");
632+
633+
// Correctness check: we still have a single component, and this component is still the
634+
// original underlying buffer.
635+
assertThat(cumulation.numComponents()).isEqualTo(1);
636+
// Replace '2' with '*', and '8' with '$'.
637+
buf.setByte(5, '*').setByte(11, '$');
638+
assertThat(cumulation.toString(US_ASCII)).isEqualTo("01*34567$9");
639+
}
640+
}
588641
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.