LCOV - code coverage report
Current view: top level - src/test/fuzz - addition_overflow.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 31 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 11 0
Branches: 0.0 % 26 0

             Branch data     Line data    Source code
       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                 :           0 : void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider)
      17                 :             : {
      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                 :             : #ifndef _MSC_VER
      28                 :             :     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                 :             :     }
      34                 :             : #endif
      35                 :             :     if (is_addition_overflow_custom) {
      36         [ #  # ]:           0 :         assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max());
      37                 :             :     } else {
      38         [ #  # ]:           0 :         const auto add{i + j};
      39         [ #  # ]:           0 :         assert(add == maybe_add.value());
      40         [ #  # ]:           0 :         assert(add == sat_add);
      41                 :             :     }
      42                 :           0 : }
      43                 :             : } // namespace
      44                 :             : 
      45         [ #  # ]:           0 : FUZZ_TARGET(addition_overflow)
      46                 :             : {
      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 : }
        

Generated by: LCOV version 2.0-1