summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Morph2022-02-05 13:07:51 -0500
committerGravatar Morph2022-02-05 13:56:21 -0500
commitec4d7f71fedb1cc117b0fb6c33cbffb699b98555 (patch)
treea76907fd120a1f963835f888b6f974618ef84456 /src
parentgeneral: Rename NewUUID to UUID, and remove the previous UUID impl (diff)
downloadyuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.gz
yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.xz
yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/common/uuid.cpp53
1 files 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 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <bit> 5#include <bit>
6#include <optional>
6#include <random> 7#include <random>
7 8
8#include <fmt/format.h> 9#include <fmt/format.h>
@@ -18,7 +19,7 @@ namespace {
18constexpr size_t RawStringSize = sizeof(UUID) * 2; 19constexpr size_t RawStringSize = sizeof(UUID) * 2;
19constexpr size_t FormattedStringSize = RawStringSize + 4; 20constexpr size_t FormattedStringSize = RawStringSize + 4;
20 21
21u8 HexCharToByte(char c) { 22std::optional<u8> HexCharToByte(char c) {
22 if (c >= '0' && c <= '9') { 23 if (c >= '0' && c <= '9') {
23 return static_cast<u8>(c - '0'); 24 return static_cast<u8>(c - '0');
24 } 25 }
@@ -29,15 +30,19 @@ u8 HexCharToByte(char c) {
29 return static_cast<u8>(c - 'A' + 10); 30 return static_cast<u8>(c - 'A' + 10);
30 } 31 }
31 ASSERT_MSG(false, "{} is not a hexadecimal digit!", c); 32 ASSERT_MSG(false, "{} is not a hexadecimal digit!", c);
32 return u8{0}; 33 return std::nullopt;
33} 34}
34 35
35std::array<u8, 0x10> ConstructFromRawString(std::string_view raw_string) { 36std::array<u8, 0x10> ConstructFromRawString(std::string_view raw_string) {
36 std::array<u8, 0x10> uuid; 37 std::array<u8, 0x10> uuid;
37 38
38 for (size_t i = 0; i < RawStringSize; i += 2) { 39 for (size_t i = 0; i < RawStringSize; i += 2) {
39 uuid[i / 2] = 40 const auto upper = HexCharToByte(raw_string[i]);
40 static_cast<u8>((HexCharToByte(raw_string[i]) << 4) | HexCharToByte(raw_string[i + 1])); 41 const auto lower = HexCharToByte(raw_string[i + 1]);
42 if (!upper || !lower) {
43 return {};
44 }
45 uuid[i / 2] = static_cast<u8>((*upper << 4) | *lower);
41 } 46 }
42 47
43 return uuid; 48 return uuid;
@@ -52,40 +57,60 @@ std::array<u8, 0x10> ConstructFromFormattedString(std::string_view formatted_str
52 const auto* str = formatted_string.data(); 57 const auto* str = formatted_string.data();
53 58
54 for (; i < 4; ++i) { 59 for (; i < 4; ++i) {
55 uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); 60 const auto upper = HexCharToByte(*(str++));
56 uuid[i] |= HexCharToByte(*(str++)); 61 const auto lower = HexCharToByte(*(str++));
62 if (!upper || !lower) {
63 return {};
64 }
65 uuid[i] = static_cast<u8>((*upper << 4) | *lower);
57 } 66 }
58 67
59 // Process the next 4 characters. 68 // Process the next 4 characters.
60 ++str; 69 ++str;
61 70
62 for (; i < 6; ++i) { 71 for (; i < 6; ++i) {
63 uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); 72 const auto upper = HexCharToByte(*(str++));
64 uuid[i] |= HexCharToByte(*(str++)); 73 const auto lower = HexCharToByte(*(str++));
74 if (!upper || !lower) {
75 return {};
76 }
77 uuid[i] = static_cast<u8>((*upper << 4) | *lower);
65 } 78 }
66 79
67 // Process the next 4 characters. 80 // Process the next 4 characters.
68 ++str; 81 ++str;
69 82
70 for (; i < 8; ++i) { 83 for (; i < 8; ++i) {
71 uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); 84 const auto upper = HexCharToByte(*(str++));
72 uuid[i] |= HexCharToByte(*(str++)); 85 const auto lower = HexCharToByte(*(str++));
86 if (!upper || !lower) {
87 return {};
88 }
89 uuid[i] = static_cast<u8>((*upper << 4) | *lower);
73 } 90 }
74 91
75 // Process the next 4 characters. 92 // Process the next 4 characters.
76 ++str; 93 ++str;
77 94
78 for (; i < 10; ++i) { 95 for (; i < 10; ++i) {
79 uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); 96 const auto upper = HexCharToByte(*(str++));
80 uuid[i] |= HexCharToByte(*(str++)); 97 const auto lower = HexCharToByte(*(str++));
98 if (!upper || !lower) {
99 return {};
100 }
101 uuid[i] = static_cast<u8>((*upper << 4) | *lower);
81 } 102 }
82 103
83 // Process the last 12 characters. 104 // Process the last 12 characters.
84 ++str; 105 ++str;
85 106
86 for (; i < 16; ++i) { 107 for (; i < 16; ++i) {
87 uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); 108 const auto upper = HexCharToByte(*(str++));
88 uuid[i] |= HexCharToByte(*(str++)); 109 const auto lower = HexCharToByte(*(str++));
110 if (!upper || !lower) {
111 return {};
112 }
113 uuid[i] = static_cast<u8>((*upper << 4) | *lower);
89 } 114 }
90 115
91 return uuid; 116 return uuid;