lp: Packet::wireDecode checks TLV-TYPE of Block before decoding
refs #3256
Change-Id: Ia0fe8f88416eeecf969cc1a585e3625efe42e866
diff --git a/src/lp/packet.cpp b/src/lp/packet.cpp
index 82b7821..c839666 100644
--- a/src/lp/packet.cpp
+++ b/src/lp/packet.cpp
@@ -97,6 +97,10 @@
return;
}
+ if (wire.type() != tlv::LpPacket) {
+ BOOST_THROW_EXCEPTION(Error("unrecognized TLV-TYPE " + to_string(wire.type())));
+ }
+
wire.parse();
bool isFirst = true;
@@ -105,7 +109,7 @@
detail::FieldInfo info(element.type());
if (!info.isRecognized && !info.canIgnore) {
- BOOST_THROW_EXCEPTION(Error("unknown field cannot be ignored"));
+ BOOST_THROW_EXCEPTION(Error("unrecognized field cannot be ignored"));
}
if (!isFirst) {
diff --git a/src/lp/packet.hpp b/src/lp/packet.hpp
index eed6b51..aa77a4f 100644
--- a/src/lp/packet.hpp
+++ b/src/lp/packet.hpp
@@ -60,6 +60,7 @@
/**
* \brief decode packet from wire format
+ * \throws Error unknown TLV-TYPE
*/
void
wireDecode(const Block& wire);
diff --git a/tests/unit-tests/lp/packet.t.cpp b/tests/unit-tests/lp/packet.t.cpp
index 4150e0c..29ec091 100644
--- a/tests/unit-tests/lp/packet.t.cpp
+++ b/tests/unit-tests/lp/packet.t.cpp
@@ -325,6 +325,13 @@
encoded.begin(), encoded.end());
}
+BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
+{
+ Packet packet;
+ Block wire = encoding::makeEmptyBlock(ndn::tlv::Name);
+ BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
+}
+
BOOST_AUTO_TEST_SUITE_END()
} // namespace tests