diff options
author | fanquake <fanquake@gmail.com> | 2023-02-27 14:21:58 +0000 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2023-02-27 14:27:50 +0000 |
commit | a2877f7ad3a58327215f782003a85a6f46486e4c (patch) | |
tree | 813877349c25ac5871a272290a2d459b7dac0e56 /src/util | |
parent | 873dcc19102f6017dac070fa83e2333f8bf6845b (diff) | |
parent | faab273e060d27e166b5fb7fe7692614ec9e5c76 (diff) |
Merge bitcoin/bitcoin#25227: Handle invalid hex encoding in ParseHex
faab273e060d27e166b5fb7fe7692614ec9e5c76 util: Return empty vector on invalid hex encoding (MarcoFalke)
fa3549a77bf6a15d8309d36056237f3126baf721 test: Add hex parse unit tests (MarcoFalke)
Pull request description:
Seems a bit confusing to happily accept random bytes and pretend they are hex encoded strings.
ACKs for top commit:
stickies-v:
re-ACK faab273e060d27e166b5fb7fe7692614ec9e5c76
Tree-SHA512: a808135f744f50aece03d4bf5a71481c7bdca1fcdd0d5b113abdb0c8b382bf81cafee6d17c239041fb49b59f4e19970f24a475378e7f711c3a47d6438de2bdab
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strencodings.cpp | 7 | ||||
-rw-r--r-- | src/util/strencodings.h | 10 |
2 files changed, 12 insertions, 5 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 45a01429e1..03459dcf20 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -78,18 +78,19 @@ bool IsHexNumber(std::string_view str) } template <typename Byte> -std::vector<Byte> ParseHex(std::string_view str) +std::optional<std::vector<Byte>> TryParseHex(std::string_view str) { std::vector<Byte> vch; auto it = str.begin(); - while (it != str.end() && it + 1 != str.end()) { + while (it != str.end()) { if (IsSpace(*it)) { ++it; continue; } auto c1 = HexDigit(*(it++)); + if (it == str.end()) return std::nullopt; auto c2 = HexDigit(*(it++)); - if (c1 < 0 || c2 < 0) break; + if (c1 < 0 || c2 < 0) return std::nullopt; vch.push_back(Byte(c1 << 4) | Byte(c2)); } return vch; diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 626e48f499..05e7b957c4 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -57,9 +57,15 @@ enum class ByteUnit : uint64_t { * @return A new string without unsafe chars */ std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT); -/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. */ +/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. Returns nullopt on invalid input. */ +template <typename Byte = std::byte> +std::optional<std::vector<Byte>> TryParseHex(std::string_view str); +/** Like TryParseHex, but returns an empty vector on invalid input. */ template <typename Byte = uint8_t> -std::vector<Byte> ParseHex(std::string_view str); +std::vector<Byte> ParseHex(std::string_view hex_str) +{ + return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{}); +} signed char HexDigit(char c); /* Returns true if each character in str is a hex character, and has an even * number of hex digits.*/ |