exclude: avoid segfault when decoding empty Exclude element

refs #1970

Change-Id: Ia262a23dd7b0ce97986bbdb78baa99539fecd3c7
diff --git a/src/exclude.cpp b/src/exclude.cpp
index 7f069eb..202ab91 100644
--- a/src/exclude.cpp
+++ b/src/exclude.cpp
@@ -21,12 +21,14 @@
  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
  */
 
-#include "common.hpp"
-
 #include "exclude.hpp"
 
+#include <boost/static_assert.hpp>
+
 namespace ndn {
 
+BOOST_STATIC_ASSERT((boost::is_base_of<tlv::Error, Exclude::Error>::value));
+
 Exclude::Exclude()
 {
 }
@@ -40,6 +42,10 @@
 size_t
 Exclude::wireEncode(EncodingImpl<T>& block) const
 {
+  if (m_exclude.empty()) {
+    throw Error("Exclude filter cannot be empty");
+  }
+
   size_t totalLength = 0;
 
   // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
@@ -87,9 +93,16 @@
 void
 Exclude::wireDecode(const Block& wire)
 {
+  if (wire.type() != tlv::Exclude)
+    throw tlv::Error("Unexpected TLV type when decoding Exclude");
+
   m_wire = wire;
   m_wire.parse();
 
+  if (m_wire.elements_size() == 0) {
+    throw Error("Exclude element cannot be empty");
+  }
+
   // Exclude ::= EXCLUDE-TYPE TLV-LENGTH Any? (NameComponent (Any)?)+
   // Any     ::= ANY-TYPE TLV-LENGTH(=0)
 
@@ -127,8 +140,6 @@
     }
 }
 
-
-
 // example: ANY /b /d ANY /f
 //
 // ordered in map as:
@@ -166,7 +177,6 @@
   return *this;
 }
 
-
 // example: ANY /b0 /d0 ANY /f0
 //
 // ordered in map as:
@@ -251,7 +261,6 @@
   return *this;
 }
 
-
 std::ostream&
 operator<<(std::ostream& os, const Exclude& exclude)
 {
@@ -271,5 +280,4 @@
   return os;
 }
 
-
 } // namespace ndn