Before throwing TooLongFrameException,should skip the bytes to be read in MqttDecoder (#11204)

Motivation:

Before throwing TooLongFrameException, should call the skipBytes method to skip the bytes to be read

Modification:
- skip bytes before throw

Result:
Actually skip the bytes when we detect too much data

Signed-off-by: xingrufei <xingrufei@sogou-inc.com>

Co-authored-by: xingrufei <xingrufei@sogou-inc.com>
This commit is contained in:
skyguard1 2021-04-29 14:13:11 +08:00 committed by GitHub
parent d22a35628d
commit c32c520edd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -98,6 +98,7 @@ public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
final Result<?> decodedVariableHeader = decodeVariableHeader(ctx, buffer, mqttFixedHeader);
variableHeader = decodedVariableHeader.value;
if (bytesRemainingInVariablePart > maxBytesInMessage) {
buffer.skipBytes(buffer.readableBytes());
throw new TooLongFrameException("too large message: " + bytesRemainingInVariablePart + " bytes");
}
bytesRemainingInVariablePart -= decodedVariableHeader.numberOfBytesConsumed;

View File

@ -334,6 +334,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
@ -356,6 +358,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
@ -378,6 +382,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateDecoderExceptionTooLargeMessage(decodedMessage);
@ -397,6 +403,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
@ -419,6 +427,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateMessageIdVariableHeader(message.variableHeader(),
@ -440,6 +450,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateMessageIdVariableHeader(message.variableHeader(),
@ -461,6 +473,8 @@ public class MqttCodecTest {
assertEquals("Expected one object but got " + out.size(), 1, out.size());
assertEquals(0, byteBuf.readableBytes());
final MqttMessage decodedMessage = (MqttMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateMessageIdVariableHeader(message.variableHeader(),