2023-08-24 12:00:16 +02:00
|
|
|
// 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>
|
fix: add missing includes and remove obsolete includes (#5562)
## Issue being fixed or feature implemented
Some headers or modules are used objects from STL without including it
directly, it cause compilation failures on some platforms for some
specific compilers such as #5554
## What was done?
Added missing includes and removed obsolete includes for `optional`,
`deque`, `tuple`, `unordered_set`, `unordered_map`, `set` and `atomic`.
Please, note, that this PR doesn't cover all cases, only cases when it
is obviously missing or obviously obsolete.
Also most of changes belongs to to dash specific code; but for cases of
original bitcoin code I keep it untouched, such as missing <map> in
`src/psbt.h`
I used this script to get a list of files/headers which looks suspicious
`./headers-scanner.sh std::optional optional`:
```bash
#!/bin/bash
set -e
function check_includes() {
obj=$1
header=$2
file=$3
used=0
included=0
grep "$obj" "$file" >/dev/null 2>/dev/null && used=1
grep "include <$header>" $file >/dev/null 2>/dev/null && included=1
if [ $used == 1 ] && [ $included == 0 ]
then echo "missing <$header> in $file"
fi
if [ $used == 0 ] && [ $included == 1 ]
then echo "obsolete <$header> in $file"
fi
}
export -f check_includes
obj=$1
header=$2
find src \( -name '*.h' -or -name '*.cpp' -or -name '*.hpp' \) -exec bash -c 'check_includes "$0" "$1" "$2"' "$obj" "$header" {} \;
```
## How Has This Been Tested?
Built code locally
## Breaking Changes
n/a
## Checklist:
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
- [x] I have assigned this pull request to a milestone
2023-09-07 16:07:02 +02:00
|
|
|
#include <optional>
|
2023-08-24 12:00:16 +02:00
|
|
|
#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.");
|
2022-02-21 15:50:59 +01:00
|
|
|
if constexpr (std::numeric_limits<T>::is_signed) {
|
2023-08-24 12:00:16 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-02-21 15:50:59 +01:00
|
|
|
template <class T>
|
|
|
|
[[nodiscard]] T SaturatingAdd(const T i, const T j) noexcept
|
|
|
|
{
|
|
|
|
if constexpr (std::numeric_limits<T>::is_signed) {
|
|
|
|
if (i > 0 && j > std::numeric_limits<T>::max() - i) {
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
}
|
|
|
|
if (i < 0 && j < std::numeric_limits<T>::min() - i) {
|
|
|
|
return std::numeric_limits<T>::min();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (std::numeric_limits<T>::max() - i < j) {
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i + j;
|
|
|
|
}
|
|
|
|
|
2023-08-24 12:00:16 +02:00
|
|
|
#endif // BITCOIN_UTIL_OVERFLOW_H
|