Skip to content

Commit

Permalink
Before throwing TooLongFrameException,should skip the bytes to be rea…
Browse files Browse the repository at this point in the history
…d in MqttDecoder (netty#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 <[email protected]>

Co-authored-by: xingrufei <[email protected]>
  • Loading branch information
2 people authored and normanmaurer committed Apr 29, 2021
1 parent 438632a commit bf721c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exceptio
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public void testConnectMessageForMqtt31TooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateConnectVariableHeader(message.variableHeader(),
Expand All @@ -348,6 +349,7 @@ public void testConnectMessageForMqtt311TooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateConnectVariableHeader(message.variableHeader(),
Expand All @@ -369,6 +371,8 @@ public void testConnAckMessageTooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateDecoderExceptionTooLargeMessage(decodedMessage);
} finally {
Expand All @@ -387,6 +391,7 @@ public void testPublishMessageTooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePublishVariableHeader(message.variableHeader(),
Expand All @@ -408,6 +413,8 @@ public void testSubscribeMessageTooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateMessageIdVariableHeader(message.variableHeader(),
(MqttMessageIdVariableHeader) decodedMessage.variableHeader());
Expand All @@ -428,6 +435,8 @@ public void testSubAckMessageTooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateMessageIdVariableHeader(message.variableHeader(),
(MqttMessageIdVariableHeader) decodedMessage.variableHeader());
Expand All @@ -448,6 +457,8 @@ public void testUnSubscribeMessageTooLarge() throws Exception {
verify(ctx).fireChannelRead(captor.capture());

final MqttMessage decodedMessage = captor.getValue();
assertEquals(0, byteBuf.readableBytes());

validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validateMessageIdVariableHeader(message.variableHeader(),
(MqttMessageIdVariableHeader) decodedMessage.variableHeader());
Expand Down

0 comments on commit bf721c8

Please sign in to comment.