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:
Ben Woosley
2019-04-04 00:39:04 -07:00
parent ba54342c9d
commit 510c6532ba
7 changed files with 48 additions and 23 deletions

View File

@@ -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};
}