Skip to content

Commit e498aef

Browse files
author
MarcoFalke
committed
Merge #20211: Use -Wswitch for TxoutType where possible
fa650ca Use -Wswitch for TxoutType where possible (MarcoFalke) fa59e0b test: Add missing script_standard_Solver_success cases (MarcoFalke) Pull request description: This removes unused `default:` cases for all `switch` statements on `TxoutType` and adds the cases (`MULTISIG`, `NULL_DATA`, `NONSTANDARD`) to `ExtractDestination` for clarity. Also, the compiler is now able to use `-Wswitch`. ACKs for top commit: practicalswift: cr ACK fa650ca: patch looks correct and `assert(false);` is better than UB :) hebasto: ACK fa650ca, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: 282458b6523bd8923a0c0f5c423d1db2dce2a2d1b1d1dae455415c6fc995bb41ce82c1f9b0a1c0dcc6d874d171e04c30eca585f147582f52c7048c140358630a
2 parents a59e7ed + fa650ca commit e498aef

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

src/script/sign.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
106106
std::vector<valtype> vSolutions;
107107
whichTypeRet = Solver(scriptPubKey, vSolutions);
108108

109-
switch (whichTypeRet)
110-
{
109+
switch (whichTypeRet) {
111110
case TxoutType::NONSTANDARD:
112111
case TxoutType::NULL_DATA:
113112
case TxoutType::WITNESS_UNKNOWN:
@@ -173,10 +172,8 @@ static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator
173172
// Could not find witnessScript, add to missing
174173
sigdata.missing_witness_script = uint256(vSolutions[0]);
175174
return false;
176-
177-
default:
178-
return false;
179-
}
175+
} // no default case, so the compiler can warn about missing cases
176+
assert(false);
180177
}
181178

182179
static CScript PushAll(const std::vector<valtype>& values)

src/script/standard.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ WitnessV0ScriptHash::WitnessV0ScriptHash(const CScript& in)
4545

4646
std::string GetTxnOutputType(TxoutType t)
4747
{
48-
switch (t)
49-
{
48+
switch (t) {
5049
case TxoutType::NONSTANDARD: return "nonstandard";
5150
case TxoutType::PUBKEY: return "pubkey";
5251
case TxoutType::PUBKEYHASH: return "pubkeyhash";
@@ -182,43 +181,51 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
182181
std::vector<valtype> vSolutions;
183182
TxoutType whichType = Solver(scriptPubKey, vSolutions);
184183

185-
if (whichType == TxoutType::PUBKEY) {
184+
switch (whichType) {
185+
case TxoutType::PUBKEY: {
186186
CPubKey pubKey(vSolutions[0]);
187187
if (!pubKey.IsValid())
188188
return false;
189189

190190
addressRet = PKHash(pubKey);
191191
return true;
192192
}
193-
else if (whichType == TxoutType::PUBKEYHASH)
194-
{
193+
case TxoutType::PUBKEYHASH: {
195194
addressRet = PKHash(uint160(vSolutions[0]));
196195
return true;
197196
}
198-
else if (whichType == TxoutType::SCRIPTHASH)
199-
{
197+
case TxoutType::SCRIPTHASH: {
200198
addressRet = ScriptHash(uint160(vSolutions[0]));
201199
return true;
202-
} else if (whichType == TxoutType::WITNESS_V0_KEYHASH) {
200+
}
201+
case TxoutType::WITNESS_V0_KEYHASH: {
203202
WitnessV0KeyHash hash;
204203
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
205204
addressRet = hash;
206205
return true;
207-
} else if (whichType == TxoutType::WITNESS_V0_SCRIPTHASH) {
206+
}
207+
case TxoutType::WITNESS_V0_SCRIPTHASH: {
208208
WitnessV0ScriptHash hash;
209209
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
210210
addressRet = hash;
211211
return true;
212-
} else if (whichType == TxoutType::WITNESS_UNKNOWN || whichType == TxoutType::WITNESS_V1_TAPROOT) {
212+
}
213+
case TxoutType::WITNESS_UNKNOWN:
214+
case TxoutType::WITNESS_V1_TAPROOT: {
213215
WitnessUnknown unk;
214216
unk.version = vSolutions[0][0];
215217
std::copy(vSolutions[1].begin(), vSolutions[1].end(), unk.program);
216218
unk.length = vSolutions[1].size();
217219
addressRet = unk;
218220
return true;
219221
}
220-
// Multisig txns have more than one address...
221-
return false;
222+
case TxoutType::MULTISIG:
223+
// Multisig txns have more than one address...
224+
case TxoutType::NULL_DATA:
225+
case TxoutType::NONSTANDARD:
226+
return false;
227+
} // no default case, so the compiler can warn about missing cases
228+
assert(false);
222229
}
223230

224231
bool ExtractDestinations(const CScript& scriptPubKey, TxoutType& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)

src/test/script_standard_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ BOOST_AUTO_TEST_CASE(script_standard_Solver_success)
107107
BOOST_CHECK_EQUAL(solutions.size(), 1U);
108108
BOOST_CHECK(solutions[0] == ToByteVector(scriptHash));
109109

110+
// TxoutType::WITNESS_V1_TAPROOT
111+
s.clear();
112+
s << OP_1 << ToByteVector(uint256::ZERO);
113+
BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::WITNESS_V1_TAPROOT);
114+
BOOST_CHECK_EQUAL(solutions.size(), 2U);
115+
BOOST_CHECK(solutions[0] == std::vector<unsigned char>{1});
116+
BOOST_CHECK(solutions[1] == ToByteVector(uint256::ZERO));
117+
118+
// TxoutType::WITNESS_UNKNOWN
119+
s.clear();
120+
s << OP_16 << ToByteVector(uint256::ONE);
121+
BOOST_CHECK_EQUAL(Solver(s, solutions), TxoutType::WITNESS_UNKNOWN);
122+
BOOST_CHECK_EQUAL(solutions.size(), 2U);
123+
BOOST_CHECK(solutions[0] == std::vector<unsigned char>{16});
124+
BOOST_CHECK(solutions[1] == ToByteVector(uint256::ONE));
125+
110126
// TxoutType::NONSTANDARD
111127
s.clear();
112128
s << OP_9 << OP_ADD << OP_11 << OP_EQUAL;

src/wallet/rpcdump.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,9 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
934934
case TxoutType::NONSTANDARD:
935935
case TxoutType::WITNESS_UNKNOWN:
936936
case TxoutType::WITNESS_V1_TAPROOT:
937-
default:
938937
return "unrecognized script";
939-
}
938+
} // no default case, so the compiler can warn about missing cases
939+
CHECK_NONFATAL(false);
940940
}
941941

942942
static UniValue ProcessImportLegacy(ImportData& import_data, std::map<CKeyID, CPubKey>& pubkey_map, std::map<CKeyID, CKey>& privkey_map, std::set<CScript>& script_pub_keys, bool& have_solving_data, const UniValue& data, std::vector<CKeyID>& ordered_pubkeys)

src/wallet/scriptpubkeyman.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ IsMineResult IsMineInner(const LegacyScriptPubKeyMan& keystore, const CScript& s
9494
TxoutType whichType = Solver(scriptPubKey, vSolutions);
9595

9696
CKeyID keyID;
97-
switch (whichType)
98-
{
97+
switch (whichType) {
9998
case TxoutType::NONSTANDARD:
10099
case TxoutType::NULL_DATA:
101100
case TxoutType::WITNESS_UNKNOWN:
@@ -194,7 +193,7 @@ IsMineResult IsMineInner(const LegacyScriptPubKeyMan& keystore, const CScript& s
194193
}
195194
break;
196195
}
197-
}
196+
} // no default case, so the compiler can warn about missing cases
198197

199198
if (ret == IsMineResult::NO && keystore.HaveWatchOnly(scriptPubKey)) {
200199
ret = std::max(ret, IsMineResult::WATCH_ONLY);

0 commit comments

Comments
 (0)