Skip to content

Commit fabdf81

Browse files
author
MarcoFalke
committed
test: Add test for embedded null in hex string
Also, fix style in the corresponding function. The style change can be reviewed with "--word-diff-regex=."
1 parent f0a834e commit fabdf81

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/test/util_tests.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static const unsigned char ParseHex_expected[65] = {
153153
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
154154
0x5f
155155
};
156-
BOOST_AUTO_TEST_CASE(util_ParseHex)
156+
BOOST_AUTO_TEST_CASE(parse_hex)
157157
{
158158
std::vector<unsigned char> result;
159159
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
@@ -169,6 +169,14 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
169169
result = ParseHex(" 89 34 56 78");
170170
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
171171

172+
// Embedded null is treated as end
173+
const std::string with_embedded_null{" 11 "s
174+
" \0 "
175+
" 22 "s};
176+
BOOST_CHECK_EQUAL(with_embedded_null.size(), 11);
177+
result = ParseHex(with_embedded_null);
178+
BOOST_CHECK(result.size() == 1 && result[0] == 0x11);
179+
172180
// Stop parsing at invalid value
173181
result = ParseHex("1234 invalid 1234");
174182
BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);

src/util/strencodings.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ bool IsHexNumber(std::string_view str)
7878

7979
std::vector<unsigned char> ParseHex(std::string_view str)
8080
{
81-
// convert hex dump to vector
8281
std::vector<unsigned char> vch;
8382
auto it = str.begin();
8483
while (it != str.end() && it + 1 != str.end()) {

0 commit comments

Comments
 (0)