Skip to content

Commit 9faa4b6

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22232: refactor: Pass interpreter flags as uint32_t instead of signed int
fa621ed refactor: Pass script verify flags as uint32_t (MarcoFalke) Pull request description: The flags are cast to unsigned in the interpreter anyway, so avoid the confusion (and fuzz crashes) by just passing them as unsigned from the beginning. Also, the flags are often inverted bit-wise with the `~` operator, which also works on signed integers, but might cause confusion as the sign bit is flipped. Fixes #22233 ACKs for top commit: theStack: Concept and code review ACK fa621ed kristapsk: ACK fa621ed jonatack: ACK fa621ed Tree-SHA512: ea0720f32f823fa7f075309978672aa39773c6019d12b6c1c9d611fc1983a76115b7fe2a28d50814673bb6415c311ccc05b99d6e871575fb6900faf75ed17769
2 parents 3d8c714 + fa621ed commit 9faa4b6

File tree

8 files changed

+16
-17
lines changed

8 files changed

+16
-17
lines changed

src/bench/verify_script.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
2121
const ECCVerifyHandle verify_handle;
2222
ECC_Start();
2323

24-
const int flags = SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH;
24+
const uint32_t flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
2525
const int witnessversion = 0;
2626

2727
// Key pair.

src/consensus/tx_verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& in
144144
return nSigOps;
145145
}
146146

147-
int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
147+
int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags)
148148
{
149149
int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;
150150

src/consensus/tx_verify.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& ma
4949
* Compute total signature operation cost of a transaction.
5050
* @param[in] tx Transaction for which we are computing the cost
5151
* @param[in] inputs Map of previous transactions that have outputs we're spending
52-
* @param[out] flags Script verification flags
52+
* @param[in] flags Script verification flags
5353
* @return Total signature operation cost of tx
5454
*/
55-
int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags);
55+
int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags);
5656

5757
/**
5858
* Check if transaction is final and can be included in a block with the

src/script/interpreter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ enum
3939
* All flags are intended to be soft forks: the set of acceptable scripts under
4040
* flags (A | B) is a subset of the acceptable scripts under flag (A).
4141
*/
42-
enum
43-
{
42+
enum : uint32_t {
4443
SCRIPT_VERIFY_NONE = 0,
4544

4645
// Evaluate P2SH subscripts (BIP16).

src/test/fuzz/coins_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
258258
// consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
259259
return;
260260
}
261-
const int flags = fuzzed_data_provider.ConsumeIntegral<int>();
261+
const auto flags{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
262262
if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
263263
// Avoid:
264264
// script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness *, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.

src/test/script_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static ScriptError_t ParseScriptError(const std::string& name)
123123

124124
BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup)
125125

126-
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, int flags, const std::string& message, int scriptError, CAmount nValue = 0)
126+
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, uint32_t flags, const std::string& message, int scriptError, CAmount nValue = 0)
127127
{
128128
bool expect = (scriptError == SCRIPT_ERR_OK);
129129
if (flags & SCRIPT_VERIFY_CLEANSTACK) {
@@ -139,8 +139,8 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
139139

140140
// Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
141141
for (int i = 0; i < 16; ++i) {
142-
int extra_flags = InsecureRandBits(16);
143-
int combined_flags = expect ? (flags & ~extra_flags) : (flags | extra_flags);
142+
uint32_t extra_flags(InsecureRandBits(16));
143+
uint32_t combined_flags{expect ? (flags & ~extra_flags) : (flags | extra_flags)};
144144
// Weed out some invalid flag combinations.
145145
if (combined_flags & SCRIPT_VERIFY_CLEANSTACK && ~combined_flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) continue;
146146
if (combined_flags & SCRIPT_VERIFY_WITNESS && ~combined_flags & SCRIPT_VERIFY_P2SH) continue;
@@ -150,7 +150,7 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
150150
#if defined(HAVE_CONSENSUS_LIB)
151151
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
152152
stream << tx2;
153-
int libconsensus_flags = flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL;
153+
uint32_t libconsensus_flags{flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL};
154154
if (libconsensus_flags == flags) {
155155
int expectedSuccessCode = expect ? 1 : 0;
156156
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
@@ -258,7 +258,7 @@ class TestBuilder
258258
bool havePush;
259259
std::vector<unsigned char> push;
260260
std::string comment;
261-
int flags;
261+
uint32_t flags;
262262
int scriptError;
263263
CAmount nValue;
264264

@@ -278,7 +278,7 @@ class TestBuilder
278278
}
279279

280280
public:
281-
TestBuilder(const CScript& script_, const std::string& comment_, int flags_, bool P2SH = false, WitnessMode wm = WitnessMode::NONE, int witnessversion = 0, CAmount nValue_ = 0) : script(script_), havePush(false), comment(comment_), flags(flags_), scriptError(SCRIPT_ERR_OK), nValue(nValue_)
281+
TestBuilder(const CScript& script_, const std::string& comment_, uint32_t flags_, bool P2SH = false, WitnessMode wm = WitnessMode::NONE, int witnessversion = 0, CAmount nValue_ = 0) : script(script_), havePush(false), comment(comment_), flags(flags_), scriptError(SCRIPT_ERR_OK), nValue(nValue_)
282282
{
283283
CScript scriptPubKey = script;
284284
if (wm == WitnessMode::PKH) {
@@ -1677,7 +1677,7 @@ static void AssetTest(const UniValue& test)
16771677
const std::vector<CTxOut> prevouts = TxOutsFromJSON(test["prevouts"]);
16781678
BOOST_CHECK(prevouts.size() == mtx.vin.size());
16791679
size_t idx = test["index"].get_int64();
1680-
unsigned int test_flags = ParseScriptFlags(test["flags"].get_str());
1680+
uint32_t test_flags{ParseScriptFlags(test["flags"].get_str())};
16811681
bool fin = test.exists("final") && test["final"].get_bool();
16821682

16831683
if (test.exists("success")) {

src/test/sigopcount_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(GetSigOpCount)
6767
* Verifies script execution of the zeroth scriptPubKey of tx output and
6868
* zeroth scriptSig and witness of tx input.
6969
*/
70-
static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, int flags)
70+
static ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, uint32_t flags)
7171
{
7272
ScriptError error;
7373
CTransaction inputi(input);
@@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
121121
key.MakeNewKey(true);
122122
CPubKey pubkey = key.GetPubKey();
123123
// Default flags
124-
int flags = SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH;
124+
const uint32_t flags{SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH};
125125

126126
// Multisig script (legacy counting)
127127
{

src/test/transaction_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const
446446
assert(input.vin[0].scriptWitness.stack == inputm.vin[0].scriptWitness.stack);
447447
}
448448

449-
static void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, int flags, bool success)
449+
static void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, uint32_t flags, bool success)
450450
{
451451
ScriptError error;
452452
CTransaction inputi(input);

0 commit comments

Comments
 (0)