blockchain: sanity check number of precomputed hash of hash blocks

Coverity 142951
This commit is contained in:
moneromooo-monero 2018-02-02 16:50:35 +00:00
parent 9af6b2d1b8
commit 61defd89e3
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3

View File

@ -4343,8 +4343,13 @@ void Blockchain::load_compiled_in_block_hashes()
{
const unsigned char *p = get_blocks_dat_start(m_testnet);
const uint32_t nblocks = *p | ((*(p+1))<<8) | ((*(p+2))<<16) | ((*(p+3))<<24);
if (nblocks > (std::numeric_limits<uint32_t>::max() - 4) / sizeof(hash))
{
MERROR("Block hash data is too large");
return;
}
const size_t size_needed = 4 + nblocks * sizeof(crypto::hash);
if(nblocks > 0 && nblocks * HASH_OF_HASHES_STEP > m_db->height() && get_blocks_dat_size(m_testnet) >= size_needed)
if(nblocks > 0 && nblocks > (m_db->height() + HASH_OF_HASHES_STEP - 1) / HASH_OF_HASHES_STEP && get_blocks_dat_size(m_testnet) >= size_needed)
{
p += sizeof(uint32_t);
m_blocks_hash_of_hashes.reserve(nblocks);