LCOV - code coverage report
Current view: top level - src/test/fuzz - addition_overflow.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 100.0 % 31 31
Test Date: 2026-11-21 05:38:30 Functions: 100.0 % 11 11
Branches: 53.8 % 26 14

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-present 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/check.h>
       9                 :             : #include <util/overflow.h>
      10                 :             : 
      11                 :             : #include <cstdint>
      12                 :             : #include <string>
      13                 :             : #include <vector>
      14                 :             : 
      15                 :             : namespace {
      16                 :             : template <typename T>
      17                 :         981 : void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
      18                 :             : {
      19                 :         981 :     const T i = fuzzed_data_provider.ConsumeIntegral<T>();
      20                 :         981 :     const T j = fuzzed_data_provider.ConsumeIntegral<T>();
      21                 :         981 :     const bool is_addition_overflow_custom = AdditionOverflow(i, j);
      22                 :         981 :     const auto maybe_add{CheckedAdd(i, j)};
      23         [ -  + ]:         981 :     const auto sat_add{SaturatingAdd(i, j)};
      24         [ -  + ]:         981 :     assert(is_addition_overflow_custom == !maybe_add.has_value());
      25         [ -  + ]:         981 :     assert(is_addition_overflow_custom == AdditionOverflow(j, i));
      26         [ +  - ]:         981 :     assert(maybe_add == CheckedAdd(j, i));
      27         [ -  + ]:         981 :     assert(sat_add == SaturatingAdd(j, i));
      28                 :             : #ifndef _MSC_VER
      29                 :             :     T result_builtin;
      30                 :         981 :     const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin);
      31         [ -  + ]:         981 :     assert(is_addition_overflow_custom == is_addition_overflow_builtin);
      32         [ +  + ]:         981 :     if (!is_addition_overflow_custom) {
      33         [ -  + ]:         501 :         assert(i + j == result_builtin);
      34                 :             :     }
      35                 :             : #endif
      36                 :             :     if (is_addition_overflow_custom) {
      37         [ -  + ]:         480 :         assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());
      38                 :             :     } else {
      39         [ +  - ]:         501 :         const auto add{i + j};
      40         [ -  + ]:         501 :         assert(add == maybe_add.value());
      41         [ -  + ]:         501 :         assert(add == sat_add);
      42                 :             :     }
      43                 :         981 : }
      44                 :             : } // namespace
      45                 :             : 
      46         [ +  - ]:         585 : FUZZ_TARGET(addition_overflow)
      47                 :             : {
      48                 :         109 :     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
      49                 :         109 :     TestAdditionOverflow<int64_t>(fuzzed_data_provider);
      50                 :         109 :     TestAdditionOverflow<uint64_t>(fuzzed_data_provider);
      51                 :         109 :     TestAdditionOverflow<int32_t>(fuzzed_data_provider);
      52                 :         109 :     TestAdditionOverflow<uint32_t>(fuzzed_data_provider);
      53                 :         109 :     TestAdditionOverflow<int16_t>(fuzzed_data_provider);
      54                 :         109 :     TestAdditionOverflow<uint16_t>(fuzzed_data_provider);
      55                 :         109 :     TestAdditionOverflow<char>(fuzzed_data_provider);
      56                 :         109 :     TestAdditionOverflow<unsigned char>(fuzzed_data_provider);
      57                 :         109 :     TestAdditionOverflow<signed char>(fuzzed_data_provider);
      58                 :         109 : }
        

Generated by: LCOV version 2.5.0-full