Skip to content

Commit c338cd6

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
(partial) Merge 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 bitcoin#22233 ACKs for top commit: theStack: Concept and code review ACK fa621ed kristapsk: ACK fa621ed jonatack: ACK fa621ed Tree-SHA512: ea0720f32f823fa7f075309978672aa39773c6019d12b6c1c9d611fc1983a76115b7fe2a28d50814673bb6415c311ccc05b99d6e871575fb6900faf75ed17769
1 parent f40cb71 commit c338cd6

File tree

5 files changed

+10
-11
lines changed

5 files changed

+10
-11
lines changed

src/consensus/tx_verify.cpp

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

148-
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
148+
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, uint32_t flags)
149149
{
150150
unsigned int nSigOps = GetLegacySigOpCount(tx);
151151

src/consensus/tx_verify.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& ma
5252
* @param[out] flags Script verification flags
5353
* @return Total signature operation count for a tx
5454
*/
55-
unsigned int GetTransactionSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs, int flags);
55+
unsigned int GetTransactionSigOpCount(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
@@ -32,8 +32,7 @@ enum
3232
* All flags are intended to be soft forks: the set of acceptable scripts under
3333
* flags (A | B) is a subset of the acceptable scripts under flag (A).
3434
*/
35-
enum
36-
{
35+
enum : uint32_t {
3736
SCRIPT_VERIFY_NONE = 0,
3837

3938
// Evaluate P2SH subscripts (BIP16).

src/test/fuzz/coins_view.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view)
251251
// consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
252252
return;
253253
}
254-
const int flags = fuzzed_data_provider.ConsumeIntegral<int>();
254+
const auto flags{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
255255
if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_P2SH) == 0) {
256256
// Avoid:
257257
// 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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static ScriptError_t ParseScriptError(const std::string& name)
123123
BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup)
124124

125125

126-
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, const std::string& message, int scriptError)
126+
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, uint32_t flags, const std::string& message, int scriptError)
127127
{
128128
bool expect = (scriptError == SCRIPT_ERR_OK);
129129
bool fEnableDIP0020Opcodes = (SCRIPT_ENABLE_DIP0020_OPCODES & flags) != 0;
@@ -136,8 +136,8 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, co
136136

137137
// Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
138138
for (int i = 0; i < 16; ++i) {
139-
int extra_flags = InsecureRandBits(16);
140-
int combined_flags = expect ? (flags & ~extra_flags) : (flags | extra_flags);
139+
uint32_t extra_flags(InsecureRandBits(16));
140+
uint32_t combined_flags{expect ? (flags & ~extra_flags) : (flags | extra_flags)};
141141
// Weed out some invalid flag combinations.
142142
if (combined_flags & SCRIPT_VERIFY_CLEANSTACK && ~combined_flags & SCRIPT_VERIFY_P2SH) continue;
143143
// Make sure DIP0020 opcodes flag stays unchanged.
@@ -148,7 +148,7 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, int flags, co
148148
#if defined(HAVE_CONSENSUS_LIB)
149149
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
150150
stream << tx2;
151-
int libconsensus_flags = flags & dashconsensus_SCRIPT_FLAGS_VERIFY_ALL;
151+
uint32_t libconsensus_flags{flags & dashconsensus_SCRIPT_FLAGS_VERIFY_ALL};
152152
if (libconsensus_flags == flags) {
153153
int expectedSuccessCode = expect ? 1 : 0;
154154
BOOST_CHECK_MESSAGE(dashconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message);
@@ -240,7 +240,7 @@ class TestBuilder
240240
bool havePush;
241241
std::vector<unsigned char> push;
242242
std::string comment;
243-
int flags;
243+
uint32_t flags;
244244
int scriptError;
245245

246246
void DoPush()
@@ -259,7 +259,7 @@ class TestBuilder
259259
}
260260

261261
public:
262-
TestBuilder(const CScript& script_, const std::string& comment_, int flags_, bool P2SH = false) : scriptPubKey(script_), havePush(false), comment(comment_), flags(flags_), scriptError(SCRIPT_ERR_OK)
262+
TestBuilder(const CScript& script_, const std::string& comment_, uint32_t flags_, bool P2SH = false) : scriptPubKey(script_), havePush(false), comment(comment_), flags(flags_), scriptError(SCRIPT_ERR_OK)
263263
{
264264
if (P2SH) {
265265
creditTx = MakeTransactionRef(BuildCreditingTransaction(CScript() << OP_HASH160 << ToByteVector(CScriptID(script_)) << OP_EQUAL));

0 commit comments

Comments
 (0)