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 — Auto-port 4.1: Reset LengthFieldBasedFrameDecoder state when the fram… · netty/netty@8aee85d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec/src
    • main/java/io/netty/handler/codec
      • LengthFieldBasedFrameDecoder.java
    • test/java/io/netty/handler/codec
      • LengthFieldBasedFrameDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec/src/main/java/io/netty/handler/codec/LengthFieldBasedFrameDecoder.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/LengthFieldBasedFrameDecoder.java
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@ protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception
430430
return null;
431431
}
432432
if (initialBytesToStrip > frameLengthInt) {
433-
failOnFrameLengthLessThanInitialBytesToStrip(in, frameLength, initialBytesToStrip);
433+
// Reset the state before failing so the next frame is decoded again. Use frameLengthInt, as frameLength
434+
// is 0 if the length field was decoded by a previous call.
435+
int frameLengthToSkip = frameLengthInt;
436+
frameLengthInt = -1;
437+
failOnFrameLengthLessThanInitialBytesToStrip(in, frameLengthToSkip, initialBytesToStrip);
434438
}
435439
in.skipBytes(initialBytesToStrip);
436440

Collapse file

‎codec/src/test/java/io/netty/handler/codec/LengthFieldBasedFrameDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/LengthFieldBasedFrameDecoderTest.java
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
import static org.junit.jupiter.api.Assertions.assertFalse;
2425
import static org.junit.jupiter.api.Assertions.assertNull;
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627
import static org.junit.jupiter.api.Assertions.fail;
@@ -86,4 +87,53 @@ public void testDiscardTooLongFrame2() {
8687

8788
buf.release();
8889
}
90+
91+
@Test
92+
public void testDecodeNextFrameAfterFrameLengthLessThanInitialBytesToStrip() {
93+
EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4, 0, 6));
94+
95+
// The adjusted frame length (4) is less than initialBytesToStrip (6).
96+
try {
97+
channel.writeInbound(Unpooled.buffer().writeInt(0));
98+
fail();
99+
} catch (CorruptedFrameException e) {
100+
// expected
101+
}
102+
103+
// The next frame must be decoded again.
104+
ByteBuf buf = Unpooled.buffer().writeInt(4).writeShort(1).writeShort(2);
105+
assertTrue(channel.writeInbound(buf));
106+
ByteBuf b = channel.readInbound();
107+
assertEquals(2, b.readableBytes());
108+
assertEquals(2, b.readShort());
109+
b.release();
110+
111+
assertNull(channel.readInbound());
112+
assertFalse(channel.finish());
113+
}
114+
115+
@Test
116+
public void testDecodeNextFrameAfterFrameLengthLessThanInitialBytesToStripInSeparateReads() {
117+
EmbeddedChannel channel = new EmbeddedChannel(new LengthFieldBasedFrameDecoder(16, 0, 4, 0, 10));
118+
119+
// The length field arrives first, and the rest of the malformed frame (adjusted length 8 is less than
120+
// initialBytesToStrip 10) arrives in a later read.
121+
assertFalse(channel.writeInbound(Unpooled.buffer().writeInt(4)));
122+
try {
123+
channel.writeInbound(Unpooled.buffer().writeInt(0));
124+
fail();
125+
} catch (CorruptedFrameException e) {
126+
// expected
127+
}
128+
129+
ByteBuf buf = Unpooled.buffer().writeInt(8).writeInt(1).writeShort(2).writeShort(3);
130+
assertTrue(channel.writeInbound(buf));
131+
ByteBuf b = channel.readInbound();
132+
assertEquals(2, b.readableBytes());
133+
assertEquals(3, b.readShort());
134+
b.release();
135+
136+
assertNull(channel.readInbound());
137+
assertFalse(channel.finish());
138+
}
89139
}

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.