mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-07 06:07:32 +02:00
refactor: add overflow-safe CeilDiv helper
Introduce `CeilDiv()` for integral ceiling division without the typical `(dividend + divisor - 1) / divisor` overflow, asserting a non-zero divisor. Replace existing ceiling-division expressions with `CeilDiv()` to centralize the preconditions. Add unit tests covering return type deduction, max-value behavior, and divisor checks.
This commit is contained in:
@@ -5,13 +5,14 @@
|
||||
|
||||
#include <merkleblock.h>
|
||||
|
||||
#include <hash.h>
|
||||
#include <consensus/consensus.h>
|
||||
#include <hash.h>
|
||||
#include <util/overflow.h>
|
||||
|
||||
|
||||
std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits)
|
||||
{
|
||||
std::vector<unsigned char> ret((bits.size() + 7) / 8);
|
||||
std::vector<unsigned char> ret(CeilDiv(bits.size(), 8u));
|
||||
for (unsigned int p = 0; p < bits.size(); p++) {
|
||||
ret[p / 8] |= bits[p] << (p % 8);
|
||||
}
|
||||
@@ -174,7 +175,7 @@ uint256 CPartialMerkleTree::ExtractMatches(std::vector<Txid> &vMatch, std::vecto
|
||||
if (fBad)
|
||||
return uint256();
|
||||
// verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
|
||||
if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
|
||||
if (CeilDiv(nBitsUsed, 8u) != CeilDiv(vBits.size(), 8u))
|
||||
return uint256();
|
||||
// verify that all hashes were consumed
|
||||
if (nHashUsed != vHash.size())
|
||||
|
||||
Reference in New Issue
Block a user