Bitcoin Core  24.99.0
P2P Digital Currency
addition_overflow.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <test/fuzz/util.h>
8 #include <util/overflow.h>
9 
10 #include <cstdint>
11 #include <string>
12 #include <vector>
13 
14 #if defined(__has_builtin)
15 #if __has_builtin(__builtin_add_overflow)
16 #define HAVE_BUILTIN_ADD_OVERFLOW
17 #endif
18 #elif defined(__GNUC__)
19 #define HAVE_BUILTIN_ADD_OVERFLOW
20 #endif
21 
22 namespace {
23 template <typename T>
24 void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
25 {
26  const T i = fuzzed_data_provider.ConsumeIntegral<T>();
27  const T j = fuzzed_data_provider.ConsumeIntegral<T>();
28  const bool is_addition_overflow_custom = AdditionOverflow(i, j);
29  const auto maybe_add{CheckedAdd(i, j)};
30  const auto sat_add{SaturatingAdd(i, j)};
31  assert(is_addition_overflow_custom == !maybe_add.has_value());
32  assert(is_addition_overflow_custom == AdditionOverflow(j, i));
33  assert(maybe_add == CheckedAdd(j, i));
34  assert(sat_add == SaturatingAdd(j, i));
35 #if defined(HAVE_BUILTIN_ADD_OVERFLOW)
36  T result_builtin;
37  const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin);
38  assert(is_addition_overflow_custom == is_addition_overflow_builtin);
39  if (!is_addition_overflow_custom) {
40  assert(i + j == result_builtin);
41  }
42 #endif
43  if (is_addition_overflow_custom) {
44  assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());
45  } else {
46  const auto add{i + j};
47  assert(add == maybe_add.value());
48  assert(add == sat_add);
49  }
50 }
51 } // namespace
52 
53 FUZZ_TARGET(addition_overflow)
54 {
55  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
56  TestAdditionOverflow<int64_t>(fuzzed_data_provider);
57  TestAdditionOverflow<uint64_t>(fuzzed_data_provider);
58  TestAdditionOverflow<int32_t>(fuzzed_data_provider);
59  TestAdditionOverflow<uint32_t>(fuzzed_data_provider);
60  TestAdditionOverflow<int16_t>(fuzzed_data_provider);
61  TestAdditionOverflow<uint16_t>(fuzzed_data_provider);
62  TestAdditionOverflow<char>(fuzzed_data_provider);
63  TestAdditionOverflow<unsigned char>(fuzzed_data_provider);
64  TestAdditionOverflow<signed char>(fuzzed_data_provider);
65 }
FUZZ_TARGET(addition_overflow)
#define T(expected, seed, data)
bool AdditionOverflow(const T i, const T j) noexcept
Definition: overflow.h:13
std::optional< T > CheckedAdd(const T i, const T j) noexcept
Definition: overflow.h:24
T SaturatingAdd(const T i, const T j) noexcept
Definition: overflow.h:33
assert(!tx.IsCoinBase())