Bitcoin Core 28.99.0
P2P Digital Currency
multiplication_overflow.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-2021 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
9#include <cstdint>
10#include <string>
11#include <vector>
12
13namespace {
14template <typename T>
15void TestMultiplicationOverflow(FuzzedDataProvider& fuzzed_data_provider)
16{
17 const T i = fuzzed_data_provider.ConsumeIntegral<T>();
18 const T j = fuzzed_data_provider.ConsumeIntegral<T>();
19 const bool is_multiplication_overflow_custom = MultiplicationOverflow(i, j);
20#ifndef _MSC_VER
21 T result_builtin;
22 const bool is_multiplication_overflow_builtin = __builtin_mul_overflow(i, j, &result_builtin);
23 assert(is_multiplication_overflow_custom == is_multiplication_overflow_builtin);
24 if (!is_multiplication_overflow_custom) {
25 assert(i * j == result_builtin);
26 }
27#else
28 if (!is_multiplication_overflow_custom) {
29 (void)(i * j);
30 }
31#endif
32}
33} // namespace
34
35FUZZ_TARGET(multiplication_overflow)
36{
37 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
38 TestMultiplicationOverflow<int64_t>(fuzzed_data_provider);
39 TestMultiplicationOverflow<uint64_t>(fuzzed_data_provider);
40 TestMultiplicationOverflow<int32_t>(fuzzed_data_provider);
41 TestMultiplicationOverflow<uint32_t>(fuzzed_data_provider);
42 TestMultiplicationOverflow<int16_t>(fuzzed_data_provider);
43 TestMultiplicationOverflow<uint16_t>(fuzzed_data_provider);
44 TestMultiplicationOverflow<char>(fuzzed_data_provider);
45 TestMultiplicationOverflow<unsigned char>(fuzzed_data_provider);
46 TestMultiplicationOverflow<signed char>(fuzzed_data_provider);
47}
#define T(expected, seed, data)
FUZZ_TARGET(multiplication_overflow)
bool MultiplicationOverflow(const T i, const T j) noexcept
Definition: util.h:208
assert(!tx.IsCoinBase())