unit-test: add language tag length validation

Ensure caller has provided enough language tag data to match with their
input length

Change-Id: Icc60601d19ee33a52413dd8f2b8462791571ebcb
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
diff --git a/slp_parser.cpp b/slp_parser.cpp
index 293cba7..b79cff2 100644
--- a/slp_parser.cpp
+++ b/slp_parser.cpp
@@ -68,19 +68,30 @@
 
         langtagLen = endian::from_network(langtagLen);
 
-        req.header.langtag.insert(
-            0, (const char*)buff.data() + slp::header::OFFSET_LANG, langtagLen);
-
-        /* check for the validity of the function */
-        if (req.header.functionID <
-                static_cast<uint8_t>(slp::FunctionType::SRVRQST) ||
-            req.header.functionID >
-                static_cast<uint8_t>(slp::FunctionType::SAADV))
+        // Enforce language tag size limits
+        if ((slp::header::OFFSET_LANG + langtagLen) > buff.size())
         {
-            std::cerr << "Invalid function ID: " << req.header.functionID
+            std::cerr << "Invalid Language Tag Length: " << langtagLen
                       << std::endl;
             rc = static_cast<int>(slp::Error::PARSE_ERROR);
         }
+        else
+        {
+            req.header.langtag.insert(
+                0, (const char*)buff.data() + slp::header::OFFSET_LANG,
+                langtagLen);
+
+            /* check for the validity of the function */
+            if (req.header.functionID <
+                    static_cast<uint8_t>(slp::FunctionType::SRVRQST) ||
+                req.header.functionID >
+                    static_cast<uint8_t>(slp::FunctionType::SAADV))
+            {
+                std::cerr << "Invalid function ID: " << req.header.functionID
+                          << std::endl;
+                rc = static_cast<int>(slp::Error::PARSE_ERROR);
+            }
+        }
     }
 
     return std::make_tuple(rc, std::move(req));
diff --git a/test/slp_parser_test.cpp b/test/slp_parser_test.cpp
index 3615c93..338b214 100644
--- a/test/slp_parser_test.cpp
+++ b/test/slp_parser_test.cpp
@@ -40,3 +40,29 @@
 
     EXPECT_NE(rc, 0);
 }
+
+TEST(parseHeaderTest, ValidLangTagLength)
+{
+    // Language Tag Length that is valid
+    slp::buffer testData{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                         0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00};
+
+    slp::Message req;
+    int rc = slp::SUCCESS;
+    std::tie(rc, req) = slp::parser::internal::parseHeader(testData);
+
+    EXPECT_EQ(rc, 0);
+}
+
+TEST(parseHeaderTest, InvalidLangTagLength)
+{
+    // Language Tag Length that is too big
+    slp::buffer testData{0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                         0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00};
+
+    slp::Message req;
+    int rc = slp::SUCCESS;
+    std::tie(rc, req) = slp::parser::internal::parseHeader(testData);
+
+    EXPECT_NE(rc, 0);
+}