Coverage Report

Created: 2024-10-21 15:10

/root/bitcoin/src/util/overflow.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2021-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
#ifndef BITCOIN_UTIL_OVERFLOW_H
6
#define BITCOIN_UTIL_OVERFLOW_H
7
8
#include <limits>
9
#include <optional>
10
#include <type_traits>
11
12
template <class T>
13
[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept
14
0
{
15
0
    static_assert(std::is_integral<T>::value, "Integral required.");
16
0
    if constexpr (std::numeric_limits<T>::is_signed) {
17
0
        return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
18
0
               (i < 0 && j < std::numeric_limits<T>::min() - i);
19
0
    }
20
0
    return std::numeric_limits<T>::max() - i < j;
21
0
}
Unexecuted instantiation: _Z16AdditionOverflowIlEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowImEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowIiEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowIjEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowIsEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowItEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowIcEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowIhEbT_S0_
Unexecuted instantiation: _Z16AdditionOverflowIaEbT_S0_
22
23
template <class T>
24
[[nodiscard]] std::optional<T> CheckedAdd(const T i, const T j) noexcept
25
0
{
26
0
    if (AdditionOverflow(i, j)) {
27
0
        return std::nullopt;
28
0
    }
29
0
    return i + j;
30
0
}
Unexecuted instantiation: _Z10CheckedAddIlESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddImESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddIiESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddIjESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddIsESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddItESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddIcESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddIhESt8optionalIT_ES1_S1_
Unexecuted instantiation: _Z10CheckedAddIaESt8optionalIT_ES1_S1_
31
32
template <class T>
33
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept
34
0
{
35
0
    if constexpr (std::numeric_limits<T>::is_signed) {
36
0
        if (i > 0 && j > std::numeric_limits<T>::max() - i) {
37
0
            return std::numeric_limits<T>::max();
38
0
        }
39
0
        if (i < 0 && j < std::numeric_limits<T>::min() - i) {
40
0
            return std::numeric_limits<T>::min();
41
0
        }
42
0
    } else {
43
0
        if (std::numeric_limits<T>::max() - i < j) {
44
0
            return std::numeric_limits<T>::max();
45
0
        }
46
0
    }
47
0
    return i + j;
48
0
}
Unexecuted instantiation: _Z13SaturatingAddIlET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddImET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddIiET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddIjET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddIsET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddItET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddIcET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddIhET_S0_S0_
Unexecuted instantiation: _Z13SaturatingAddIaET_S0_S0_
49
50
#endif // BITCOIN_UTIL_OVERFLOW_H