mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
Extract ParseDescriptorRange
So as to be consistently informative when the checks fail, and to protect against unintentional divergence among the checks.
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
#include <tinyformat.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <tuple>
|
||||
|
||||
InitInterfaces* g_rpc_interfaces = nullptr;
|
||||
|
||||
// Converts a hex string to a public key if possible
|
||||
@@ -529,7 +531,7 @@ std::string RPCArg::ToString(const bool oneline) const
|
||||
assert(false);
|
||||
}
|
||||
|
||||
std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
|
||||
static std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
|
||||
{
|
||||
if (value.isNum()) {
|
||||
return {0, value.get_int64()};
|
||||
@@ -542,3 +544,19 @@ std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
|
||||
}
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
|
||||
}
|
||||
|
||||
std::pair<int64_t, int64_t> ParseDescriptorRange(const UniValue& value)
|
||||
{
|
||||
int64_t low, high;
|
||||
std::tie(low, high) = ParseRange(value);
|
||||
if (low < 0) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should be greater or equal than 0");
|
||||
}
|
||||
if ((high >> 31) != 0) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "End of range is too high");
|
||||
}
|
||||
if (high >= low + 1000000) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range is too large");
|
||||
}
|
||||
return {low, high};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user