Skip to content

Commit 09ce350

Browse files
committed
fix: Make TxidFromString() respect string_view length
Prior to this, passing string_view::data() into uint256S() meant the latter would only receive the a naked char* pointer and potentially scan past the string_view::length() until it found a null terminator (or some other non-hex character). Appears to have been a fully dormant bug as callers were either passing a string literal or std::string directly to TxidFromFromString(), meaning null terminator always existed at pointer[length()]. Bug existed since original merge of TxidFromString(), discussed in bitcoin/bitcoin#28922 (comment).
1 parent 01e314c commit 09ce350

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

src/test/transaction_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,10 +1030,10 @@ BOOST_AUTO_TEST_CASE(test_IsStandard)
10301030

10311031
BOOST_AUTO_TEST_CASE(test_TxidFromString)
10321032
{
1033-
// TxidFromString currently ignores string_view length and reads the whole
1034-
// string, not the specified substring.
1033+
// Make sure TxidFromString respects string_view length and stops reading at
1034+
// end of the substring.
10351035
BOOST_CHECK_EQUAL(TxidFromString(std::string_view("ABCD1234", 4)).ToString(),
1036-
"00000000000000000000000000000000000000000000000000000000abcd1234");
1036+
"000000000000000000000000000000000000000000000000000000000000abcd");
10371037
}
10381038

10391039
BOOST_AUTO_TEST_SUITE_END()

src/test/txpackage_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ inline CTransactionRef create_placeholder_tx(size_t num_inputs, size_t num_outpu
4747
// Create a Wtxid from a hex string
4848
inline Wtxid WtxidFromString(std::string_view str)
4949
{
50-
return Wtxid::FromUint256(uint256S(str.data()));
50+
return Wtxid::FromUint256(uint256S(str));
5151
}
5252

5353
BOOST_FIXTURE_TEST_CASE(package_hash_tests, TestChain100Setup)

src/util/transaction_identifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ using Wtxid = transaction_identifier<true>;
6868

6969
inline Txid TxidFromString(std::string_view str)
7070
{
71-
return Txid::FromUint256(uint256S(str.data()));
71+
return Txid::FromUint256(uint256S(str));
7272
}
7373

7474
#endif // BITCOIN_UTIL_TRANSACTION_IDENTIFIER_H

0 commit comments

Comments
 (0)