Skip to content

Commit fafab8e

Browse files
author
MarcoFalke
committed
bitcoin-tx: Reject non-integral and out of range sequence ids
1 parent fa53d3d commit fafab8e

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/bitcoin-tx.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ static void MutateTxRBFOptIn(CMutableTransaction& tx, const std::string& strInId
235235
}
236236
}
237237

238+
template <typename T>
239+
static T TrimAndParse(const std::string& int_str, const std::string& err)
240+
{
241+
const auto parsed{ToIntegral<T>(TrimString(int_str))};
242+
if (!parsed.has_value()) {
243+
throw std::runtime_error(err + " '" + int_str + "'");
244+
}
245+
return parsed.value();
246+
}
247+
238248
static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInput)
239249
{
240250
std::vector<std::string> vStrInputParts;
@@ -261,8 +271,9 @@ static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInpu
261271

262272
// extract the optional sequence number
263273
uint32_t nSequenceIn = CTxIn::SEQUENCE_FINAL;
264-
if (vStrInputParts.size() > 2)
265-
nSequenceIn = std::stoul(vStrInputParts[2]);
274+
if (vStrInputParts.size() > 2) {
275+
nSequenceIn = TrimAndParse<uint32_t>(vStrInputParts.at(2), "invalid TX sequence id");
276+
}
266277

267278
// append to transaction input list
268279
CTxIn txin(txid, vout, CScript(), nSequenceIn);

test/lint/lint-locale-dependence.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export LC_ALL=C
4141
# independent ToIntegral<T>(...) or the ParseInt*() functions.
4242
# TODO: Reduce KNOWN_VIOLATIONS by replacing uses of locale dependent snprintf with strprintf.
4343
KNOWN_VIOLATIONS=(
44-
"src/bitcoin-tx.cpp.*stoul"
4544
"src/dbwrapper.cpp:.*vsnprintf"
4645
"src/rest.cpp:.*strtol"
4746
"src/test/dbwrapper_tests.cpp:.*snprintf"

test/util/data/bitcoin-util-test.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,30 @@
515515
"output_cmp": "txcreatedata2.json",
516516
"description": "Creates a new transaction with one input, one address output and one data (zero value) output (output in json)"
517517
},
518+
{ "exec": "./bitcoin-tx",
519+
"args":
520+
["-create",
521+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:11aa"],
522+
"return_code": 1,
523+
"error_txt": "error: invalid TX sequence id '11aa'",
524+
"description": "Try to parse a sequence number outside the allowed range"
525+
},
526+
{ "exec": "./bitcoin-tx",
527+
"args":
528+
["-create",
529+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:-1"],
530+
"return_code": 1,
531+
"error_txt": "error: invalid TX sequence id '-1'",
532+
"description": "Try to parse a sequence number outside the allowed range"
533+
},
534+
{ "exec": "./bitcoin-tx",
535+
"args":
536+
["-create",
537+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0:4294967296"],
538+
"return_code": 1,
539+
"error_txt": "error: invalid TX sequence id '4294967296'",
540+
"description": "Try to parse a sequence number outside the allowed range"
541+
},
518542
{ "exec": "./bitcoin-tx",
519543
"args":
520544
["-create",

0 commit comments

Comments
 (0)