Bitcoin Core  22.99.0
P2P Digital Currency
multiplication_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 
5 #if defined(HAVE_CONFIG_H)
7 #endif
8 
10 #include <test/fuzz/fuzz.h>
11 #include <test/fuzz/util.h>
12 
13 #include <cstdint>
14 #include <string>
15 #include <vector>
16 
17 namespace {
18 template <typename T>
19 void TestMultiplicationOverflow(FuzzedDataProvider& fuzzed_data_provider)
20 {
21  const T i = fuzzed_data_provider.ConsumeIntegral<T>();
22  const T j = fuzzed_data_provider.ConsumeIntegral<T>();
23  const bool is_multiplication_overflow_custom = MultiplicationOverflow(i, j);
24 #if defined(HAVE_BUILTIN_MUL_OVERFLOW)
25  T result_builtin;
26  const bool is_multiplication_overflow_builtin = __builtin_mul_overflow(i, j, &result_builtin);
27  assert(is_multiplication_overflow_custom == is_multiplication_overflow_builtin);
28  if (!is_multiplication_overflow_custom) {
29  assert(i * j == result_builtin);
30  }
31 #else
32  if (!is_multiplication_overflow_custom) {
33  (void)(i * j);
34  }
35 #endif
36 }
37 } // namespace
38 
39 FUZZ_TARGET(multiplication_overflow)
40 {
41  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
42  TestMultiplicationOverflow<int64_t>(fuzzed_data_provider);
43  TestMultiplicationOverflow<uint64_t>(fuzzed_data_provider);
44  TestMultiplicationOverflow<int32_t>(fuzzed_data_provider);
45  TestMultiplicationOverflow<uint32_t>(fuzzed_data_provider);
46  TestMultiplicationOverflow<int16_t>(fuzzed_data_provider);
47  TestMultiplicationOverflow<uint16_t>(fuzzed_data_provider);
48  TestMultiplicationOverflow<char>(fuzzed_data_provider);
49  TestMultiplicationOverflow<unsigned char>(fuzzed_data_provider);
50  TestMultiplicationOverflow<signed char>(fuzzed_data_provider);
51 }
assert
assert(!tx.IsCoinBase())
util.h
bitcoin-config.h
MultiplicationOverflow
bool MultiplicationOverflow(const T i, const T j) noexcept
Definition: util.h:172
FuzzedDataProvider.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
T
#define T(expected, seed, data)
FUZZ_TARGET
FUZZ_TARGET(multiplication_overflow)
Definition: multiplication_overflow.cpp:39