This is a slightly different API from our checked integer arithmetics, and will make other backports much easier.
Backport of core#24224 and core#35372.
Differential D20439
util: Add SaturatingAdd helper Authored by Fabien on Aug 5 2026, 08:23. Project Tags None Referenced Files
Subscribers None
Details
This is a slightly different API from our checked integer arithmetics, and will make other backports much easier. Backport of core#24224 and core#35372. ninja all check ninja bitcoin-fuzzers
Diff Detail
Event TimelineComment Actions Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed. Diff : committed changes only (\(\ ──────────────────────────────────────────────────────────────────────── minor [Functional Correctness]
→ ]8;;vscode://file//work/src/util/overflow.h:31src/util/overflow.h:31-46]8;;
Restrict SaturatingAdd to integral types.
SaturatingAdd compiles but has incorrect behavior. For floating-point
types, numeric_limits::min() is positive. A normal negative addition can
therefore return a positive value. Match the integral-only contract
already enforced by AdditionOverflow.
Proposed fix
template <class T>
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept {
+ static_assert(std::is_integral<T>::value, "Integral required.");
if constexpr (std::numeric_limits<T>::is_signed) {──────────────────────────────────────── Minor 1 3 files reviewed:
──────────────────────────────────────── Print all AI prompts: coderabbit review --show-prompts Comment Actions The build failed due to an unexpected infrastructure outage. The administrators have been notified to investigate. Sorry for the inconvenience. Comment Actions Build Bitcoin ABC Diffs / Diff Testing (ai-review) passed. Diff : committed changes only (\(\ ──────────────────────────────────────── 3 files reviewed:
──────────────────────────────────────── |