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 — ProtobufVarint32FrameDecoder must wait for more data when the length … · netty/netty@335aed7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-protobuf/src
    • main/java/io/netty/handler/codec/protobuf
      • ProtobufVarint32FrameDecoder.java
    • test/java/io/netty/handler/codec/protobuf
      • ProtobufVarint32FrameDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoder.java‎

Copy file name to clipboardExpand all lines: codec-protobuf/src/main/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoder.java
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,13 @@ static int readRawVarint32(ByteBuf buffer) {
139139
}
140140

141141
private static int readRawVarint40(ByteBuf buffer, int wholeOrMore) {
142-
byte lastByte;
143-
if (buffer.readableBytes() == 4 || (lastByte = buffer.getByte(buffer.readerIndex() + 4)) < 0) {
142+
if (buffer.readableBytes() == 4) {
143+
// The first 4 bytes all have the continuation bit set, but the 5th byte did not arrive yet.
144+
// Leave the readerIndex untouched so the caller waits for more data.
145+
return 0;
146+
}
147+
byte lastByte = buffer.getByte(buffer.readerIndex() + 4);
148+
if (lastByte < 0) {
144149
throw new CorruptedFrameException("malformed varint.");
145150
}
146151
buffer.skipBytes(5);
@@ -178,6 +183,9 @@ private static int readRawVarint24(ByteBuf buffer) {
178183
if ((tmp = buffer.readByte()) >= 0) {
179184
return result | tmp << 14;
180185
}
181-
return result | (tmp & 127) << 14;
186+
// All 3 readable bytes have the continuation bit set, which means at least one more byte is needed to
187+
// complete the varint. We only end up here if less than 4 bytes were readable, so wait for more data.
188+
buffer.resetReaderIndex();
189+
return 0;
182190
}
183191
}
Collapse file

‎codec-protobuf/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-protobuf/src/test/java/io/netty/handler/codec/protobuf/ProtobufVarint32FrameDecoderTest.java
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.netty.buffer.ByteBuf;
1919
import io.netty.channel.embedded.EmbeddedChannel;
20+
import io.netty.handler.codec.CorruptedFrameException;
2021
import io.netty.handler.codec.TooLongFrameException;
2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
@@ -144,4 +145,83 @@ public void testValidFrameAfterOversized() {
144145
expected.release();
145146
actual.release();
146147
}
148+
149+
@Test
150+
public void testLengthPrefixSplitAtEveryPosition() {
151+
// Cover 1, 2, 3 and 4 byte prefixes, including lengths whose low 21 bits are all zero (multiples of 2 MiB).
152+
// For these the partial varint read of the first 3 prefix bytes evaluates to 0.
153+
int[] lengths = {
154+
1, 127,
155+
128, 16383,
156+
16384, 2097151,
157+
2097152, 2097152 + 5, 3 * 1024 * 1024, 4 * 1024 * 1024
158+
};
159+
for (int length : lengths) {
160+
ByteBuf wire = buffer();
161+
ProtobufVarint32LengthFieldPrepender.writeRawVarint32(wire, length);
162+
int prefixLength = wire.readableBytes();
163+
byte[] payload = new byte[length];
164+
// Mark the last byte so a frame that starts or ends at the wrong position is detected.
165+
payload[length - 1] = 1;
166+
wire.writeBytes(payload);
167+
ByteBuf expected = wire.slice(prefixLength, length);
168+
169+
for (int split = 1; split <= prefixLength; split++) {
170+
EmbeddedChannel channel = new EmbeddedChannel(new ProtobufVarint32FrameDecoder());
171+
String message = "length: " + length + ", split after: " + split;
172+
assertFalse(channel.writeInbound(wire.retainedSlice(0, split)), message);
173+
assertTrue(channel.writeInbound(wire.retainedSlice(split, wire.readableBytes() - split)), message);
174+
175+
ByteBuf actual = channel.readInbound();
176+
try {
177+
assertEquals(length, actual.readableBytes(), message);
178+
assertEquals(expected, actual, message);
179+
} finally {
180+
actual.release();
181+
}
182+
assertNull(channel.readInbound(), message);
183+
assertFalse(channel.finish(), message);
184+
}
185+
wire.release();
186+
}
187+
}
188+
189+
@Test
190+
public void testFiveByteLengthPrefixSplitAtEveryPosition() {
191+
// 0x10000000 needs a 5 byte prefix: 80 80 80 80 01. Use a small maxFrameLength so we do not need to
192+
// allocate the whole frame, the TooLongFrameException proves the complete prefix was decoded.
193+
byte[] prefix = { (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x01 };
194+
for (int split = 1; split < prefix.length; split++) {
195+
EmbeddedChannel channel = new EmbeddedChannel(new ProtobufVarint32FrameDecoder(1024));
196+
assertFalse(channel.writeInbound(wrappedBuffer(prefix, 0, split)), "split after: " + split);
197+
final ByteBuf remaining = wrappedBuffer(prefix, split, prefix.length - split);
198+
TooLongFrameException e = assertThrows(TooLongFrameException.class,
199+
() -> channel.writeInbound(remaining), "split after: " + split);
200+
assertTrue(e.getMessage().contains(String.valueOf(0x10000000)), e.getMessage());
201+
assertFalse(channel.finish());
202+
}
203+
}
204+
205+
@Test
206+
public void testReadRawVarint32WaitsForIncompleteVarint() {
207+
byte[][] incomplete = {
208+
{ (byte) 0x80 },
209+
{ (byte) 0x80, (byte) 0x80 },
210+
{ (byte) 0x80, (byte) 0x80, (byte) 0x80 },
211+
{ (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 }
212+
};
213+
for (byte[] bytes : incomplete) {
214+
ByteBuf buf = wrappedBuffer(bytes);
215+
ProtobufVarint32FrameDecoder.readRawVarint32(buf);
216+
assertEquals(0, buf.readerIndex(), "readerIndex must not move for " + bytes.length + " incomplete bytes");
217+
buf.release();
218+
}
219+
}
220+
221+
@Test
222+
public void testReadRawVarint32RejectsSixByteVarint() {
223+
ByteBuf buf = wrappedBuffer(new byte[] { (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 });
224+
assertThrows(CorruptedFrameException.class, () -> ProtobufVarint32FrameDecoder.readRawVarint32(buf));
225+
buf.release();
226+
}
147227
}

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.