From ec4d7f71fedb1cc117b0fb6c33cbffb699b98555 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sat, 5 Feb 2022 13:07:51 -0500 Subject: [PATCH] common: uuid: Return an invalid UUID if conversion from string fails The string constructor of UUID states: Should the input string not meet the above requirements, an assert will be triggered and an invalid UUID is set instead. --- src/common/uuid.cpp | 53 +++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/common/uuid.cpp b/src/common/uuid.cpp index 10a1b86e0..4aab10e08 100644 --- a/src/common/uuid.cpp +++ b/src/common/uuid.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include @@ -18,7 +19,7 @@ namespace { constexpr size_t RawStringSize = sizeof(UUID) * 2; constexpr size_t FormattedStringSize = RawStringSize + 4; -u8 HexCharToByte(char c) { +std::optional HexCharToByte(char c) { if (c >= '0' && c <= '9') { return static_cast(c - '0'); } @@ -29,15 +30,19 @@ u8 HexCharToByte(char c) { return static_cast(c - 'A' + 10); } ASSERT_MSG(false, "{} is not a hexadecimal digit!", c); - return u8{0}; + return std::nullopt; } std::array ConstructFromRawString(std::string_view raw_string) { std::array uuid; for (size_t i = 0; i < RawStringSize; i += 2) { - uuid[i / 2] = - static_cast((HexCharToByte(raw_string[i]) << 4) | HexCharToByte(raw_string[i + 1])); + const auto upper = HexCharToByte(raw_string[i]); + const auto lower = HexCharToByte(raw_string[i + 1]); + if (!upper || !lower) { + return {}; + } + uuid[i / 2] = static_cast((*upper << 4) | *lower); } return uuid; @@ -52,40 +57,60 @@ std::array ConstructFromFormattedString(std::string_view formatted_str const auto* str = formatted_string.data(); for (; i < 4; ++i) { - uuid[i] = static_cast((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast((*upper << 4) | *lower); } // Process the next 4 characters. ++str; for (; i < 6; ++i) { - uuid[i] = static_cast((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast((*upper << 4) | *lower); } // Process the next 4 characters. ++str; for (; i < 8; ++i) { - uuid[i] = static_cast((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast((*upper << 4) | *lower); } // Process the next 4 characters. ++str; for (; i < 10; ++i) { - uuid[i] = static_cast((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast((*upper << 4) | *lower); } // Process the last 12 characters. ++str; for (; i < 16; ++i) { - uuid[i] = static_cast((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast((*upper << 4) | *lower); } return uuid;