Skip to content

Commit 81bfb5a

Browse files
committed
add checks for deserialization errors
1 parent 232aa9e commit 81bfb5a

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/test/sighash_tests.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,27 +177,36 @@ BOOST_AUTO_TEST_CASE(sighash_from_data)
177177
}
178178
if (test.size() == 1) continue; // comment
179179

180-
std::string raw_tx = test[0].get_str();
181-
std::string raw_script = test[1].get_str();
182-
int nIn = test[2].get_int();
183-
int nHashType = test[3].get_int();
184-
std::string sigHashHex = test[4].get_str();
185-
180+
std::string raw_tx, raw_script, sigHashHex;
181+
int nIn, nHashType;
186182
uint256 sh;
187-
CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION);
188183
CTransaction tx;
189-
stream >> tx;
190-
191-
CValidationState state;
192-
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
193-
BOOST_CHECK(state.IsValid());
194-
195184
CScript scriptCode = CScript();
196-
std::vector<unsigned char> raw = ParseHex(raw_script);
197-
scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
198185

186+
try {
187+
// deserialize test data
188+
raw_tx = test[0].get_str();
189+
raw_script = test[1].get_str();
190+
nIn = test[2].get_int();
191+
nHashType = test[3].get_int();
192+
sigHashHex = test[4].get_str();
193+
194+
uint256 sh;
195+
CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION);
196+
stream >> tx;
197+
198+
CValidationState state;
199+
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
200+
BOOST_CHECK(state.IsValid());
201+
202+
std::vector<unsigned char> raw = ParseHex(raw_script);
203+
scriptCode.insert(scriptCode.end(), raw.begin(), raw.end());
204+
} catch (...) {
205+
BOOST_ERROR("Bad test, couldn't deserialize data: " << strTest);
206+
continue;
207+
}
208+
199209
sh = SignatureHash(scriptCode, tx, nIn, nHashType);
200-
201210
BOOST_CHECK_MESSAGE(sh.GetHex() == sigHashHex, strTest);
202211
}
203212
}

0 commit comments

Comments
 (0)