mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-02 01:04:43 +02:00
Move AdditionOverflow to util, Add CheckedAdd with unit tests
This commit is contained in:
31
src/util/overflow.h
Normal file
31
src/util/overflow.h
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright (c) 2021 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_UTIL_OVERFLOW_H
|
||||
#define BITCOIN_UTIL_OVERFLOW_H
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
template <class T>
|
||||
[[nodiscard]] bool AdditionOverflow(const T i, const T j) noexcept
|
||||
{
|
||||
static_assert(std::is_integral<T>::value, "Integral required.");
|
||||
if (std::numeric_limits<T>::is_signed) {
|
||||
return (i > 0 && j > std::numeric_limits<T>::max() - i) ||
|
||||
(i < 0 && j < std::numeric_limits<T>::min() - i);
|
||||
}
|
||||
return std::numeric_limits<T>::max() - i < j;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
[[nodiscard]] std::optional<T> CheckedAdd(const T i, const T j) noexcept
|
||||
{
|
||||
if (AdditionOverflow(i, j)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return i + j;
|
||||
}
|
||||
|
||||
#endif // BITCOIN_UTIL_OVERFLOW_H
|
||||
Reference in New Issue
Block a user