Correctly reset offset when fail lazy because of too long frame. (#8257)

Motivation:

We need to reset the offset to 0 when we fail lazy because of a too long frame.

Modifications:

- Reset offset
- Add testcase

Result:

Fixes https://github.com/netty/netty/issues/8256.
This commit is contained in:
Norman Maurer 2018-09-04 19:13:56 +02:00 committed by GitHub
parent 379a56ca49
commit dc1b511fcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -137,6 +137,8 @@ public class LineBasedFrameDecoder extends ByteToMessageDecoder {
} else { } else {
discardedBytes += buffer.readableBytes(); discardedBytes += buffer.readableBytes();
buffer.readerIndex(buffer.writerIndex()); buffer.readerIndex(buffer.writerIndex());
// We skip everything in the buffer, we need to set the offset to 0 again.
offset = 0;
} }
return null; return null;
} }

View File

@ -185,4 +185,27 @@ public class LineBasedFrameDecoderTest {
buf.release(); buf.release();
buf2.release(); buf2.release();
} }
@Test
public void testNotFailFast() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(2, false, false));
assertFalse(ch.writeInbound(wrappedBuffer(new byte[] { 0, 1, 2 })));
assertFalse(ch.writeInbound(wrappedBuffer(new byte[]{ 3, 4 })));
try {
ch.writeInbound(wrappedBuffer(new byte[] { '\n' }));
fail();
} catch (TooLongFrameException expected) {
// Expected once we received a full frame.
}
assertFalse(ch.writeInbound(wrappedBuffer(new byte[] { '5' })));
assertTrue(ch.writeInbound(wrappedBuffer(new byte[] { '\n' })));
ByteBuf expected = wrappedBuffer(new byte[] { '5', '\n' });
ByteBuf buffer = ch.readInbound();
assertEquals(expected, buffer);
expected.release();
buffer.release();
assertFalse(ch.finish());
}
} }