Handle out-of-bound exception for toHex API
toHex API throws out-of-bound error if provided input length/size is
greater than 15.
This commit adds code to handle the above error case.
Change-Id: Icd4eb2e3655389e194f621fff9d6df251b851c0a
Signed-off-by: Rekha Aparna <vrekhaaparna@ibm.com>
diff --git a/vpd-manager/include/utility/common_utility.hpp b/vpd-manager/include/utility/common_utility.hpp
index b4d164f..9573aca 100644
--- a/vpd-manager/include/utility/common_utility.hpp
+++ b/vpd-manager/include/utility/common_utility.hpp
@@ -40,12 +40,14 @@
/** @brief Return the hex representation of the incoming byte.
*
* @param [in] i_aByte - The input byte.
- * @returns Hex representation of the byte as a character.
+ * @returns Null character if input byte is out of bound else returns hex
+ * representation of the byte as a character.
*/
-constexpr auto toHex(size_t i_aByte)
+constexpr auto toHex(const size_t& i_aByte) noexcept
{
constexpr auto l_map = "0123456789abcdef";
- return l_map[i_aByte];
+
+ return (i_aByte < std::strlen(l_map)) ? l_map[i_aByte] : '\0';
}
/**
diff --git a/vpd-manager/include/utility/vpd_specific_utility.hpp b/vpd-manager/include/utility/vpd_specific_utility.hpp
index 4a821d2..fa8c3fc 100644
--- a/vpd-manager/include/utility/vpd_specific_utility.hpp
+++ b/vpd-manager/include/utility/vpd_specific_utility.hpp
@@ -193,13 +193,46 @@
{
l_result.clear();
size_t l_firstByte = i_keyword[0];
- l_result += commonUtility::toHex(l_firstByte >> 4);
- l_result += commonUtility::toHex(l_firstByte & 0x0f);
+
+ auto l_hexValue = commonUtility::toHex(l_firstByte >> 4);
+
+ if (!l_hexValue)
+ {
+ throw std::runtime_error("Out of bound error");
+ }
+
+ l_result += l_hexValue;
+
+ l_hexValue = commonUtility::toHex(l_firstByte & 0x0f);
+
+ if (!l_hexValue)
+ {
+ throw std::runtime_error("Out of bound error");
+ }
+
+ l_result += l_hexValue;
+
for (size_t i = 1; i < i_keyword.size(); ++i)
{
l_result += ":";
- l_result += commonUtility::toHex(i_keyword[i] >> 4);
- l_result += commonUtility::toHex(i_keyword[i] & 0x0f);
+
+ l_hexValue = commonUtility::toHex(i_keyword[i] >> 4);
+
+ if (!l_hexValue)
+ {
+ throw std::runtime_error("Out of bound error");
+ }
+
+ l_result += l_hexValue;
+
+ l_hexValue = commonUtility::toHex(i_keyword[i] & 0x0f);
+
+ if (!l_hexValue)
+ {
+ throw std::runtime_error("Out of bound error");
+ }
+
+ l_result += l_hexValue;
}
}
else if (i_encoding == "DATE")