Skip to content

Commit 43d36ff

Browse files
committed
merge bitcoin#25227: Handle invalid hex encoding in ParseHex
1 parent fd9a92d commit 43d36ff

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/test/util_tests.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,52 @@ BOOST_AUTO_TEST_CASE(parse_hex)
145145
// Basic test vector
146146
result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
147147
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
148+
result = TryParseHex<uint8_t>("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").value();
149+
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
148150

149151
// Spaces between bytes must be supported
150152
result = ParseHex("12 34 56 78");
151153
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
154+
result = TryParseHex<uint8_t>("12 34 56 78").value();
155+
BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
152156

153157
// Leading space must be supported (used in BerkeleyEnvironment::Salvage)
154158
result = ParseHex(" 89 34 56 78");
155159
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
160+
result = TryParseHex<uint8_t>(" 89 34 56 78").value();
161+
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
162+
163+
// Mixed case and spaces are supported
164+
result = ParseHex(" Ff aA ");
165+
BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
166+
result = TryParseHex<uint8_t>(" Ff aA ").value();
167+
BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa);
156168

157-
// Embedded null is treated as end
169+
// Empty string is supported
170+
result = ParseHex("");
171+
BOOST_CHECK(result.size() == 0);
172+
result = TryParseHex<uint8_t>("").value();
173+
BOOST_CHECK(result.size() == 0);
174+
175+
// Spaces between nibbles is treated as invalid
176+
BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0);
177+
BOOST_CHECK(!TryParseHex("AAF F").has_value());
178+
179+
// Embedded null is treated as invalid
158180
const std::string with_embedded_null{" 11 "s
159181
" \0 "
160182
" 22 "s};
161183
BOOST_CHECK_EQUAL(with_embedded_null.size(), 11);
162-
result = ParseHex(with_embedded_null);
163-
BOOST_CHECK(result.size() == 1 && result[0] == 0x11);
184+
BOOST_CHECK_EQUAL(ParseHex(with_embedded_null).size(), 0);
185+
BOOST_CHECK(!TryParseHex(with_embedded_null).has_value());
186+
187+
// Non-hex is treated as invalid
188+
BOOST_CHECK_EQUAL(ParseHex("1234 invalid 1234").size(), 0);
189+
BOOST_CHECK(!TryParseHex("1234 invalid 1234").has_value());
164190

165-
// Stop parsing at invalid value
166-
result = ParseHex("1234 invalid 1234");
167-
BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
191+
// Truncated input is treated as invalid
192+
BOOST_CHECK_EQUAL(ParseHex("12 3").size(), 0);
193+
BOOST_CHECK(!TryParseHex("12 3").has_value());
168194
}
169195

170196
BOOST_AUTO_TEST_CASE(util_HexStr)

src/util/strencodings.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,19 @@ bool IsHexNumber(std::string_view str)
7979
}
8080

8181
template <typename Byte>
82-
std::vector<Byte> ParseHex(std::string_view str)
82+
std::optional<std::vector<Byte>> TryParseHex(std::string_view str)
8383
{
8484
std::vector<Byte> vch;
8585
auto it = str.begin();
86-
while (it != str.end() && it + 1 != str.end()) {
86+
while (it != str.end()) {
8787
if (IsSpace(*it)) {
8888
++it;
8989
continue;
9090
}
9191
auto c1 = HexDigit(*(it++));
92+
if (it == str.end()) return std::nullopt;
9293
auto c2 = HexDigit(*(it++));
93-
if (c1 < 0 || c2 < 0) break;
94+
if (c1 < 0 || c2 < 0) return std::nullopt;
9495
vch.push_back(Byte(c1 << 4) | Byte(c2));
9596
}
9697
return vch;

src/util/strencodings.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,15 @@ enum class ByteUnit : uint64_t {
5858
* @return A new string without unsafe chars
5959
*/
6060
std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
61-
/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. */
61+
/** Parse the hex string into bytes (uint8_t or std::byte). Ignores whitespace. Returns nullopt on invalid input. */
62+
template <typename Byte = std::byte>
63+
std::optional<std::vector<Byte>> TryParseHex(std::string_view str);
64+
/** Like TryParseHex, but returns an empty vector on invalid input. */
6265
template <typename Byte = uint8_t>
63-
std::vector<Byte> ParseHex(std::string_view str);
66+
std::vector<Byte> ParseHex(std::string_view hex_str)
67+
{
68+
return TryParseHex<Byte>(hex_str).value_or(std::vector<Byte>{});
69+
}
6470
signed char HexDigit(char c);
6571
/* Returns true if each character in str is a hex character, and has an even
6672
* number of hex digits.*/

0 commit comments

Comments
 (0)