mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-10 15:47:17 +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:
@@ -9,6 +9,7 @@
|
||||
#include <script/interpreter.h>
|
||||
#include <script/solver.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/overflow.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -72,7 +73,7 @@ public:
|
||||
return {};
|
||||
}
|
||||
std::vector<unsigned char> data = {(unsigned char)id.GetWitnessVersion()};
|
||||
data.reserve(1 + (program.size() * 8 + 4) / 5);
|
||||
data.reserve(1 + CeilDiv(program.size() * 8, 5u));
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, program.begin(), program.end());
|
||||
return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user