util: Require integers for SaturatingAdd() and AdditionOverflow()
What changed, and why it matters
This change tightens two low-level math helper functions so they only accept whole-number (integer) types. Previously, a non-integer type could slip through and silently produce wrong min/max values because the generic fallback for numeric limits returns 0. The patch prevents that misuse at compile time rather than relying on an internal check.
Treat as a defensive hardening improvement. Review all call sites of AdditionOverflow() and SaturatingAdd() to confirm no non-integer callers existed in prior releases, and include this change in routine backports for supported branches.
Security signals we found
Compile-time type constraint added to prevent non-integer numeric types from being used in overflow helpers
Removes reliance on std::numeric_limits generic fallback that returns 0 for min()/max()
Potential silent wrong-result path eliminated for AdditionOverflow and SaturatingAdd
Evidence from the diff
The commit changes AdditionOverflow() and SaturatingAdd() in src/util/overflow.h from unconstrained templates (template
Changed components
src/util/overflow.hAdditionOverflow()SaturatingAdd()Inspect captured patch +2 / −3
diff --git a/src/util/overflow.h b/src/util/overflow.h
index 274ba045..48732838 100644
--- a/src/util/overflow.h
+++ b/src/util/overflow.h
@@ -13,10 +13,9 @@
#include <optional>
#include <type_traits>
-template <class T>
+template <std::integral T>
[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept
{
- static_assert(std::is_integral_v<T>, "Integral required.");
if constexpr (std::numeric_limits<T>::is_signed) {
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
(i < 0 && j < std::numeric_limits<T>::min() - i);
@@ -41,7 +40,7 @@ template <std::unsigned_integral T, std::unsigned_integral U>
return true;
}
-template <class T>
+template <std::integral T>
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept
{
if constexpr (std::numeric_limits<T>::is_signed) {
Why this scored 33/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.