mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-23 13:12:22 +02:00
rest: Return error when header count is not integral
This commit is contained in:
12
src/rest.cpp
12
src/rest.cpp
@@ -189,9 +189,10 @@ static bool rest_headers(const std::any& context,
|
||||
if (path.size() != 2)
|
||||
return RESTERR(req, HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
|
||||
|
||||
long count = strtol(path[0].c_str(), nullptr, 10);
|
||||
if (count < 1 || count > 2000)
|
||||
const auto parsed_count{ToIntegral<size_t>(path[0])};
|
||||
if (!parsed_count.has_value() || *parsed_count < 1 || *parsed_count > 2000) {
|
||||
return RESTERR(req, HTTP_BAD_REQUEST, "Header count out of range: " + path[0]);
|
||||
}
|
||||
|
||||
std::string hashStr = path[1];
|
||||
uint256 hash;
|
||||
@@ -199,8 +200,8 @@ static bool rest_headers(const std::any& context,
|
||||
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
|
||||
|
||||
const CBlockIndex* tip = nullptr;
|
||||
std::vector<const CBlockIndex *> headers;
|
||||
headers.reserve(count);
|
||||
std::vector<const CBlockIndex*> headers;
|
||||
headers.reserve(*parsed_count);
|
||||
{
|
||||
ChainstateManager* maybe_chainman = GetChainman(context, req);
|
||||
if (!maybe_chainman) return false;
|
||||
@@ -211,8 +212,9 @@ static bool rest_headers(const std::any& context,
|
||||
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
|
||||
while (pindex != nullptr && active_chain.Contains(pindex)) {
|
||||
headers.push_back(pindex);
|
||||
if (headers.size() == (unsigned long)count)
|
||||
if (headers.size() == *parsed_count) {
|
||||
break;
|
||||
}
|
||||
pindex = active_chain.Next(pindex);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user