Skip to content

Commit 73a0d6d

Browse files
committed
Merge bitcoin/bitcoin#25611: univalue: Avoid brittle, narrowing and verbose integral type confusions
fa23c19 univalue: Avoid narrowing and verbose int constructors (MacroFake) fa3a9a1 rpc: Select int-UniValue constructor for enum value in upgradewallet RPC (MacroFake) Pull request description: As UniValue provides several constructors for integral types, the compiler is unable to select one if the passed type does not exactly match. This is unintuitive for developers and forces them to write verbose and brittle code. (Refer to `-Wnarrowing` compiler warning) For example, there are many places where an unsigned int is cast to a signed int. While the cast is safe in practice, it is still needlessly verbose and confusing as the value can never be negative. In fact it might even be unsafe if the unsigned value is large enough to map to a negative signed one. Fix this issue and other (minor) type issues. ACKs for top commit: aureleoules: ACK fa23c19. Tree-SHA512: 7d99b5b90c7d8eed2e3448167255a59e817dd6b8fcfc1b17c69ddefd0db33d1bf4344fbcd8b7f8685b58182c0f572ab9ffa99467afa666ac21843df7ea645033
2 parents c991132 + fa23c19 commit 73a0d6d

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

src/rpc/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static RPCHelpMan getconnectioncount()
6060
NodeContext& node = EnsureAnyNodeContext(request.context);
6161
const CConnman& connman = EnsureConnman(node);
6262

63-
return (int)connman.GetNodeCount(ConnectionDirection::Both);
63+
return connman.GetNodeCount(ConnectionDirection::Both);
6464
},
6565
};
6666
}
@@ -640,9 +640,9 @@ static RPCHelpMan getnetworkinfo()
640640
obj.pushKV("timeoffset", GetTimeOffset());
641641
if (node.connman) {
642642
obj.pushKV("networkactive", node.connman->GetNetworkActive());
643-
obj.pushKV("connections", (int)node.connman->GetNodeCount(ConnectionDirection::Both));
644-
obj.pushKV("connections_in", (int)node.connman->GetNodeCount(ConnectionDirection::In));
645-
obj.pushKV("connections_out", (int)node.connman->GetNodeCount(ConnectionDirection::Out));
643+
obj.pushKV("connections", node.connman->GetNodeCount(ConnectionDirection::Both));
644+
obj.pushKV("connections_in", node.connman->GetNodeCount(ConnectionDirection::In));
645+
obj.pushKV("connections_out", node.connman->GetNodeCount(ConnectionDirection::Out));
646646
}
647647
obj.pushKV("networks", GetNetworksInfo());
648648
obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()));

src/univalue/include/univalue.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,25 @@ class UniValue {
2424
typ = initialType;
2525
val = initialStr;
2626
}
27-
UniValue(uint64_t val_) {
28-
setInt(val_);
29-
}
30-
UniValue(int64_t val_) {
31-
setInt(val_);
32-
}
33-
UniValue(bool val_) {
34-
setBool(val_);
35-
}
36-
UniValue(int val_) {
37-
setInt(val_);
38-
}
39-
UniValue(double val_) {
40-
setFloat(val_);
41-
}
42-
UniValue(const std::string& val_) {
43-
setStr(val_);
44-
}
45-
UniValue(const char *val_) {
46-
std::string s(val_);
47-
setStr(s);
27+
template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,
28+
std::enable_if_t<std::is_floating_point_v<T> || // setFloat
29+
std::is_same_v<bool, T> || // setBool
30+
std::is_signed_v<T> || std::is_unsigned_v<T> || // setInt
31+
std::is_constructible_v<std::string, T>, // setStr
32+
bool> = true>
33+
UniValue(Ref&& val)
34+
{
35+
if constexpr (std::is_floating_point_v<T>) {
36+
setFloat(val);
37+
} else if constexpr (std::is_same_v<bool, T>) {
38+
setBool(val);
39+
} else if constexpr (std::is_signed_v<T>) {
40+
setInt(int64_t{val});
41+
} else if constexpr (std::is_unsigned_v<T>) {
42+
setInt(uint64_t{val});
43+
} else {
44+
setStr(std::string{std::forward<Ref>(val)});
45+
}
4846
}
4947

5048
void clear();

src/wallet/rpc/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ static RPCHelpMan upgradewallet()
532532
"\nUpgrade the wallet. Upgrades to the latest version if no version number is specified.\n"
533533
"New keys may be generated and a new wallet backup will need to be made.",
534534
{
535-
{"version", RPCArg::Type::NUM, RPCArg::Default{FEATURE_LATEST}, "The version number to upgrade to. Default is the latest wallet version."}
535+
{"version", RPCArg::Type::NUM, RPCArg::Default{int{FEATURE_LATEST}}, "The version number to upgrade to. Default is the latest wallet version."}
536536
},
537537
RPCResult{
538538
RPCResult::Type::OBJ, "", "",

0 commit comments

Comments
 (0)