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
.
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
.
Concept ACK: -Wswitch
is good, and termination via assert(false);
is better than UB :)
From the standard: “Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.” (C++11, [stmt.return])
933@@ -934,9 +934,9 @@ static std::string RecurseImportData(const CScript& script, ImportData& import_d
934 case TxoutType::NONSTANDARD:
935 case TxoutType::WITNESS_UNKNOWN:
936 case TxoutType::WITNESS_V1_TAPROOT:
937- default:
938 return "unrecognized script";
939- }
940+ } // no default case, so the compiler can warn about missing cases
941+ CHECK_NONFATAL(false);
TxoutType
is an enum class
, and its set of values does not depend on user’s input. Therefore, I think the usual assert(false);
should be used here.
CHECK_NONFATAL
in all rpc code ;)
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviewers, this pull request conflicts with the following ones:
addresses
and reqSigs
from rpc outputs by mjdietzx)If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Ack https://github.com/bitcoin/bitcoin/pull/20211/commits/fa650ca7f19307a9237e64ac311488c8947fc12a
We do switch (dest.which()) {
in a few places as well. This seems pretty similar, and I’m wondering if this should be refactored as well? (Probably in a different PR though?)
In #20286 I added TxoutType& typeRet
to ExtractDestination
here, and I was wondering if this would allow us to make these switch (dest.which()) {
statements more readable by doing it on TxoutType
as done in this PR. Thoughts, and once #20286 is merged, is this something that should be done?
We do switch (dest.which()) { in a few places as well. This seems pretty similar, and I’m wondering if this should be refactored as well? (Probably in a different PR though?)
What do you suggest? which
decays the enum class
into an integer. The compiler can’t do -Wswitch
static analysis on those switch statements.
Well once we have (would require #20286):
0bool ExtractDestination(const CScript& scriptPubKey, TxoutType& typeRet, CTxDestination& addressRet);
Could we do something like:
0TxoutType type;
1CTxDestination dest;
2ExtractDestination(m_script, type, dest);
3switch (type) {
4case TxoutType::PUBKEYHASH:
5case TxoutType::SCRIPTHASH: return OutputType::LEGACY;
6case TxoutType::WITNESS_V0_SCRIPTHASH:
7case TxoutType::WITNESS_V0_KEYHASH:
8case TxoutType::WITNESS_V1_TAPROOT:
9case TxoutType::WITNESS_UNKNOWN: return OutputType::BECH32;
10case TxoutType::PUBKEY:
11case TxoutType::MULTISIG:
12case TxoutType::NONSTANDARD:
13case TxoutType::NULL_DATA: return nullopt; // no default case, so the compiler can warn about missing cases
14}
15assert(false);
Note: I messed around with this, and probably made some mistakes here, but didn’t see any unit tests or functional tests fail. So maybe some test coverage is needed here as well?
would require #20286
Let’s discuss this in the other pull then
assert(false);
is better than UB :)