/root/bitcoin/src/test/fuzz/addition_overflow.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2020-2022 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 | | |
5 | | #include <test/fuzz/FuzzedDataProvider.h> |
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 | | namespace { |
15 | | template <typename T> |
16 | | void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider) |
17 | 0 | { |
18 | 0 | const T i = fuzzed_data_provider.ConsumeIntegral<T>(); |
19 | 0 | const T j = fuzzed_data_provider.ConsumeIntegral<T>(); |
20 | 0 | const bool is_addition_overflow_custom = AdditionOverflow(i, j); |
21 | 0 | const auto maybe_add{CheckedAdd(i, j)}; |
22 | 0 | const auto sat_add{SaturatingAdd(i, j)}; |
23 | 0 | assert(is_addition_overflow_custom == !maybe_add.has_value()); |
24 | 0 | assert(is_addition_overflow_custom == AdditionOverflow(j, i)); |
25 | 0 | assert(maybe_add == CheckedAdd(j, i)); |
26 | 0 | assert(sat_add == SaturatingAdd(j, i)); |
27 | 0 | #ifndef _MSC_VER |
28 | 0 | T result_builtin; |
29 | 0 | const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin); |
30 | 0 | assert(is_addition_overflow_custom == is_addition_overflow_builtin); |
31 | 0 | if (!is_addition_overflow_custom) { |
32 | 0 | assert(i + j == result_builtin); |
33 | 0 | } |
34 | 0 | #endif |
35 | 0 | if (is_addition_overflow_custom) { |
36 | 0 | assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max()); |
37 | 0 | } else { |
38 | 0 | const auto add{i + j}; |
39 | 0 | assert(add == maybe_add.value()); |
40 | 0 | assert(add == sat_add); |
41 | 0 | } |
42 | 0 | } Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIlEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowImEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIiEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIjEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIsEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowItEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIcEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIhEEvR18FuzzedDataProvider Unexecuted instantiation: addition_overflow.cpp:_ZN12_GLOBAL__N_120TestAdditionOverflowIaEEvR18FuzzedDataProvider |
43 | | } // namespace |
44 | | |
45 | | FUZZ_TARGET(addition_overflow) |
46 | 0 | { |
47 | 0 | FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
48 | 0 | TestAdditionOverflow<int64_t>(fuzzed_data_provider); |
49 | 0 | TestAdditionOverflow<uint64_t>(fuzzed_data_provider); |
50 | 0 | TestAdditionOverflow<int32_t>(fuzzed_data_provider); |
51 | 0 | TestAdditionOverflow<uint32_t>(fuzzed_data_provider); |
52 | 0 | TestAdditionOverflow<int16_t>(fuzzed_data_provider); |
53 | 0 | TestAdditionOverflow<uint16_t>(fuzzed_data_provider); |
54 | 0 | TestAdditionOverflow<char>(fuzzed_data_provider); |
55 | 0 | TestAdditionOverflow<unsigned char>(fuzzed_data_provider); |
56 | 0 | TestAdditionOverflow<signed char>(fuzzed_data_provider); |
57 | 0 | } |