mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-12 00:27:53 +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,8 +5,9 @@
|
||||
|
||||
#include <arith_uint256.h>
|
||||
|
||||
#include <uint256.h>
|
||||
#include <crypto/common.h>
|
||||
#include <uint256.h>
|
||||
#include <util/overflow.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
@@ -194,7 +195,7 @@ arith_uint256& arith_uint256::SetCompact(uint32_t nCompact, bool* pfNegative, bo
|
||||
|
||||
uint32_t arith_uint256::GetCompact(bool fNegative) const
|
||||
{
|
||||
int nSize = (bits() + 7) / 8;
|
||||
int nSize = CeilDiv(bits(), 8u);
|
||||
uint32_t nCompact = 0;
|
||||
if (nSize <= 3) {
|
||||
nCompact = GetLow64() << 8 * (3 - nSize);
|
||||
|
||||
Reference in New Issue
Block a user