Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
overflow.cpp
Go to the documentation of this file.
1// Copyright (c) 2025-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6#include <test/fuzz/fuzz.h>
7#include <util/check.h>
8#include <util/overflow.h>
9
10#include <algorithm>
11#include <limits>
12#include <optional>
13
14namespace {
16template <typename T, typename W>
18{
19 constexpr auto min{std::numeric_limits<T>::min()};
20 constexpr auto max{std::numeric_limits<T>::max()};
21 // Range needs to be at least twice as big to allow two numbers to be added without overflowing.
22 static_assert(min >= std::numeric_limits<W>::min() / 2);
23 static_assert(max <= std::numeric_limits<W>::max() / 2);
24
25 auto widen = [](T value) -> W { return value; };
26 auto clamp = [](W value) -> W { return std::clamp<W>(value, min, max); };
27 auto check = [](W value) -> std::optional<W> { if (value >= min && value <= max) return value; else return std::nullopt; };
28
31 const unsigned shift = fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, std::numeric_limits<W>::digits - std::numeric_limits<T>::digits);
32
33 Assert(clamp(widen(i) + widen(j)) == SaturatingAdd(i, j));
34 Assert(check(widen(i) + widen(j)) == CheckedAdd(i, j));
35
38}
39} // namespace
40
#define Assert(val)
Identity function.
Definition check.h:113
T ConsumeIntegralInRange(T min, T max)
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define T(expected, seed, data)
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition overflow.h:26
constexpr T SaturatingLeftShift(T input, unsigned shift) noexcept
Left bit shift with safe minimum and maximum values.
Definition overflow.h:86
T SaturatingAdd(const T i, const T j) noexcept
Definition overflow.h:43
constexpr std::optional< T > CheckedLeftShift(T input, unsigned shift) noexcept
Left bit shift with overflow checking.
Definition overflow.h:67
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:38