Skip to content

Commit de301b0

Browse files
committed
Merge #10130: bitcoin-tx input verification (awemany, jnewbery)
19ecd1e Add tests for bitcoin-tx input checking (John Newbery) 21704f6 Check stderr when testing bitcoin-tx (John Newbery) eb66bf9 bitcoin-tx: Fix missing range check (Awemany) Tree-SHA512: 08c6153cf7dd5e0ecd23e24d81af4c0f17534d484179dd91dcd78d42df14c91284341d31cc695469a64c507bce72c34231748b7cabb7df8f1051d228fb0a62c5
2 parents 8e4f7e7 + 19ecd1e commit de301b0

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/bitcoin-tx.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ static void MutateTxAddOutAddr(CMutableTransaction& tx, const std::string& strIn
242242
std::vector<std::string> vStrInputParts;
243243
boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
244244

245+
if (vStrInputParts.size() != 2)
246+
throw std::runtime_error("TX output missing or too many separators");
247+
245248
// Extract and validate VALUE
246249
CAmount value = ExtractAndValidateValue(vStrInputParts[0]);
247250

@@ -264,6 +267,9 @@ static void MutateTxAddOutPubKey(CMutableTransaction& tx, const std::string& str
264267
std::vector<std::string> vStrInputParts;
265268
boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
266269

270+
if (vStrInputParts.size() < 2 || vStrInputParts.size() > 3)
271+
throw std::runtime_error("TX output missing or too many separators");
272+
267273
// Extract and validate VALUE
268274
CAmount value = ExtractAndValidateValue(vStrInputParts[0]);
269275

test/util/bctest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ def bctest(testDir, testObj, buildenv):
102102
logging.error("Return code mismatch for " + outputFn)
103103
raise Exception
104104

105+
if "error_txt" in testObj:
106+
want_error = testObj["error_txt"]
107+
# Compare error text
108+
# TODO: ideally, we'd compare the strings exactly and also assert
109+
# That stderr is empty if no errors are expected. However, bitcoin-tx
110+
# emits DISPLAY errors when running as a windows application on
111+
# linux through wine. Just assert that the expected error text appears
112+
# somewhere in stderr.
113+
if want_error not in outs[1]:
114+
logging.error("Error mismatch:\n" + "Expected: " + want_error + "\nReceived: " + outs[1].rstrip())
115+
raise Exception
116+
105117
def bctester(testDir, input_basename, buildenv):
106118
""" Loads and parses the input file, runs all tests and reports results"""
107119
input_filename = testDir + "/" + input_basename

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"args": ["-", "delin=31"],
4343
"input": "tx394b54bb.hex",
4444
"return_code": 1,
45+
"error_txt": "error: Invalid TX input index '31'",
4546
"description": "Attempts to delete an input with a bad index from a transaction. Expected to fail."
4647
},
4748
{ "exec": "./bitcoin-tx",
@@ -60,6 +61,7 @@
6061
"args": ["-", "delout=2"],
6162
"input": "tx394b54bb.hex",
6263
"return_code": 1,
64+
"error_txt": "error: Invalid TX output index '2'",
6365
"description": "Attempts to delete an output with a bad index from a transaction. Expected to fail."
6466
},
6567
{ "exec": "./bitcoin-tx",
@@ -74,6 +76,38 @@
7476
"output_cmp": "tt-locktime317000-out.json",
7577
"description": "Adds an nlocktime to a transaction (output in json)"
7678
},
79+
{ "exec": "./bitcoin-tx",
80+
"args":
81+
["-create",
82+
"outaddr=1"],
83+
"return_code": 1,
84+
"error_txt": "error: TX output missing or too many separators",
85+
"description": "Malformed outaddr argument (no address specified). Expected to fail."
86+
},
87+
{ "exec": "./bitcoin-tx",
88+
"args":
89+
["-create",
90+
"outaddr=1:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o:garbage"],
91+
"return_code": 1,
92+
"error_txt": "error: TX output missing or too many separators",
93+
"description": "Malformed outaddr argument (too many separators). Expected to fail."
94+
},
95+
{ "exec": "./bitcoin-tx",
96+
"args":
97+
["-create",
98+
"outpubkey=0"],
99+
"return_code": 1,
100+
"error_txt": "error: TX output missing or too many separators",
101+
"description": "Malformed outpubkey argument (no pubkey specified). Expected to fail."
102+
},
103+
{ "exec": "./bitcoin-tx",
104+
"args":
105+
["-create",
106+
"outpubkey=0:02a5613bd857b7048924264d1e70e08fb2a7e6527d32b7ab1bb993ac59964ff397:W:non53nse"],
107+
"return_code": 1,
108+
"error_txt": "error: TX output missing or too many separators",
109+
"description": "Malformed outpubkey argument (too many separators). Expected to fail."
110+
},
77111
{ "exec": "./bitcoin-tx",
78112
"args":
79113
["-create",
@@ -233,6 +267,7 @@
233267
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0",
234268
"outdata=4:badhexdata"],
235269
"return_code": 1,
270+
"error_txt": "error: invalid TX output data",
236271
"description": "Attempts to create a new transaction with one input and an output with malformed hex data. Expected to fail"
237272
},
238273
{ "exec": "./bitcoin-tx",
@@ -241,6 +276,7 @@
241276
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0",
242277
"outdata=badhexdata"],
243278
"return_code": 1,
279+
"error_txt": "error: invalid TX output data",
244280
"description": "Attempts to create a new transaction with one input and an output with no value and malformed hex data. Expected to fail"
245281
},
246282
{ "exec": "./bitcoin-tx",

0 commit comments

Comments
 (0)