From: Milos Fabian Date: Tue, 15 Jul 2014 14:25:13 +0000 (+0200) Subject: Bug-1370: Reads message body bytes as well as exception is thrown during parsing. X-Git-Tag: release/helium~132^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=0bb1e71281337e30aea57bd2cf150ca73aa9a6a1;p=bgpcep.git Bug-1370: Reads message body bytes as well as exception is thrown during parsing. Change-Id: I661d598ec662c7445aec93d56229eae3e67e1bd2 Signed-off-by: Milos Fabian --- diff --git a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java index 3da6c16613..146ac29f06 100644 --- a/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java +++ b/bgp/parser-impl/src/test/java/org/opendaylight/protocol/bgp/parser/impl/ParserTest.java @@ -17,15 +17,12 @@ import static org.junit.matchers.JUnitMatchers.containsString; import com.google.common.collect.Lists; import com.google.common.collect.Maps; - import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; - import java.net.UnknownHostException; import java.util.Arrays; import java.util.List; import java.util.Map; - import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; @@ -83,6 +80,13 @@ public class ParserTest { (byte) 0x40, (byte) 0x04, (byte) 0x00, (byte) 0x47, (byte) 0x02, (byte) 0x06, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01 }; + public static final byte[] updateMsg = { + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x51, 0x02, 0x00, 0x00, 0x00, 0x36, 0x40, 0x01, 0x01, 0x00, 0x40, 0x02, + 0x1a, 0x02, 0x06, 0x00, 0x00, (byte) 0xfe, 0x55, 0x00, 0x00, (byte) 0xfc, 0x12, 0x00, 0x00, 0x00, 0x6d, 0x00, 0x00, 0x02, (byte) 0xbd, 0x00, 0x00, 0x0d, 0x1c, 0x00, 0x00, 0x6a, 0x74, 0x40, 0x03, + 0x04, 0x0a, 0x20, 0x00, (byte) 0xfe, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x04, 0x00, 0x00, 0x00, 0x64, 0x18, (byte) 0xa8, (byte) 0xa1, (byte) 0xf7 + }; + static MessageRegistry reg; @BeforeClass @@ -306,4 +310,17 @@ public class ParserTest { // the capabilities can be swapped. assertTrue(Arrays.equals(openWithCpblt1, ByteArray.getAllBytes(result)) || Arrays.equals(openWithCpblt2, ByteArray.getAllBytes(result))); } + + // https://bugs.opendaylight.org/show_bug.cgi?id=1370 + // tests if all bytes are read after deserialization error occurs + @Test + public void testUpdateMsgParser() throws BGPParsingException { + final ByteBuf buffer = Unpooled.copiedBuffer(updateMsg); + try { + ParserTest.reg.parseMessage(buffer); + fail(); + } catch(BGPDocumentedException e) { + assertEquals(0, buffer.readableBytes()); + } + } } diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java index d1ea5e5eee..99922413a1 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AbstractMessageRegistry.java @@ -9,11 +9,8 @@ package org.opendaylight.protocol.bgp.parser.spi; import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; - import io.netty.buffer.ByteBuf; - import java.util.Arrays; - import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; import org.opendaylight.protocol.bgp.parser.BGPError; import org.opendaylight.protocol.bgp.parser.BGPParsingException; @@ -57,11 +54,16 @@ public abstract class AbstractMessageRegistry implements MessageRegistry { throw new BGPParsingException("Size doesn't match size specified in header. Passed: " + msgBody.readableBytes() + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH) + ". "); } - final Notification msg = parseBody(messageType, msgBody, messageLength); + Notification msg = null; + try { + msg = parseBody(messageType, msgBody, messageLength); + } finally { + // Always reads body bytes + buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH); + } if (msg == null) { throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE, new byte[] { typeBytes }); } - buffer.skipBytes(messageLength - MessageUtil.COMMON_HEADER_LENGTH); return msg; }