Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <test/fuzz/FuzzedDataProvider.h>6#include <test/fuzz/fuzz.h>7#include <test/fuzz/util.h>8#include <util/overflow.h>10#include <cstdint>11#include <string>12#include <vector>14namespace {15template <typename T>16void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)17{18const T i = fuzzed_data_provider.ConsumeIntegral<T>();19const T j = fuzzed_data_provider.ConsumeIntegral<T>();20const bool is_addition_overflow_custom = AdditionOverflow(i, j);21const auto maybe_add{CheckedAdd(i, j)};22const auto sat_add{SaturatingAdd(i, j)};23assert(is_addition_overflow_custom == !maybe_add.has_value());24assert(is_addition_overflow_custom == AdditionOverflow(j, i));25assert(maybe_add == CheckedAdd(j, i));26assert(sat_add == SaturatingAdd(j, i));27#ifndef _MSC_VER28T result_builtin;29const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin);30assert(is_addition_overflow_custom == is_addition_overflow_builtin);31if (!is_addition_overflow_custom) {32assert(i + j == result_builtin);33}34#endif35if (is_addition_overflow_custom) {36assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());37} else {38const auto add{i + j};39assert(add == maybe_add.value());40assert(add == sat_add);41}42}43} // namespace45FUZZ_TARGET(addition_overflow)46{47FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());48TestAdditionOverflow<int64_t>(fuzzed_data_provider);49TestAdditionOverflow<uint64_t>(fuzzed_data_provider);50TestAdditionOverflow<int32_t>(fuzzed_data_provider);51TestAdditionOverflow<uint32_t>(fuzzed_data_provider);52TestAdditionOverflow<int16_t>(fuzzed_data_provider);53TestAdditionOverflow<uint16_t>(fuzzed_data_provider);54TestAdditionOverflow<char>(fuzzed_data_provider);55TestAdditionOverflow<unsigned char>(fuzzed_data_provider);56TestAdditionOverflow<signed char>(fuzzed_data_provider);57}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.