From 02d047fd5b93d96f159db2b8e95fc39450505159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Wed, 28 Jan 2026 14:45:30 +0100 Subject: [PATCH] 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. --- src/arith_uint256.cpp | 5 +++-- src/common/bloom.cpp | 5 +++-- src/cuckoocache.h | 3 ++- src/flatfile.cpp | 5 +++-- src/key_io.cpp | 3 ++- src/merkleblock.cpp | 7 ++++--- src/rest.cpp | 3 ++- src/serialize.h | 3 ++- src/support/allocators/pool.h | 3 ++- src/test/util_tests.cpp | 37 +++++++++++++++++++++++++++++++++++ src/util/bitdeque.h | 3 ++- src/util/bitset.h | 3 ++- src/util/feefrac.h | 3 ++- src/util/overflow.h | 16 +++++++++++++++ src/util/strencodings.cpp | 5 +++-- 15 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/arith_uint256.cpp b/src/arith_uint256.cpp index 392f052c0af..545139900a5 100644 --- a/src/arith_uint256.cpp +++ b/src/arith_uint256.cpp @@ -5,8 +5,9 @@ #include -#include #include +#include +#include #include @@ -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); diff --git a/src/common/bloom.cpp b/src/common/bloom.cpp index efb4178cab5..3ee78994c14 100644 --- a/src/common/bloom.cpp +++ b/src/common/bloom.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -166,7 +167,7 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou * restrict it to the range 1-50. */ nHashFuncs = std::max(1, std::min((int)round(logFpRate / log(0.5)), 50)); /* In this rolling bloom filter, we'll store between 2 and 3 generations of nElements / 2 entries. */ - nEntriesPerGeneration = (nElements + 1) / 2; + nEntriesPerGeneration = CeilDiv(nElements, 2u); uint32_t nMaxElements = nEntriesPerGeneration * 3; /* The maximum fpRate = pow(1.0 - exp(-nHashFuncs * nMaxElements / nFilterBits), nHashFuncs) * => pow(fpRate, 1.0 / nHashFuncs) = 1.0 - exp(-nHashFuncs * nMaxElements / nFilterBits) @@ -182,7 +183,7 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou * treated as set in generation 1, 2, or 3 respectively. * These bits are stored in separate integers: position P corresponds to bit * (P & 63) of the integers data[(P >> 6) * 2] and data[(P >> 6) * 2 + 1]. */ - data.resize(((nFilterBits + 63) / 64) << 1); + data.resize(CeilDiv(nFilterBits, 64u) << 1); reset(); } diff --git a/src/cuckoocache.h b/src/cuckoocache.h index 281efbd0696..e25f691341f 100644 --- a/src/cuckoocache.h +++ b/src/cuckoocache.h @@ -6,6 +6,7 @@ #define BITCOIN_CUCKOOCACHE_H #include +#include #include #include @@ -63,7 +64,7 @@ public: explicit bit_packed_atomic_flags(uint32_t size) { // pad out the size if needed - size = (size + 7) / 8; + size = CeilDiv(size, 8u); mem.reset(new std::atomic[size]); for (uint32_t i = 0; i < size; ++i) mem[i].store(0xFF); diff --git a/src/flatfile.cpp b/src/flatfile.cpp index 056fb9c1f92..b1b1e045660 100644 --- a/src/flatfile.cpp +++ b/src/flatfile.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include @@ -59,8 +60,8 @@ size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_ { out_of_space = false; - unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size; - unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size; + unsigned int n_old_chunks = CeilDiv(pos.nPos, m_chunk_size); + unsigned int n_new_chunks = CeilDiv(pos.nPos + add_size, m_chunk_size); if (n_new_chunks > n_old_chunks) { size_t old_size = pos.nPos; size_t new_size = n_new_chunks * m_chunk_size; diff --git a/src/key_io.cpp b/src/key_io.cpp index 3726d22233f..94964bdd8d5 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -9,6 +9,7 @@ #include