diff options
author | MarcoFalke <falke.marco@gmail.com> | 2022-02-21 15:50:59 +0100 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2022-02-21 15:51:15 +0100 |
commit | e44423c9d364eba9199b7097eeb20b0dfa91284e (patch) | |
tree | 78d95015a1b4b80428a433525d3225daefd9dcbd /src/test/fuzz | |
parent | bd6b1d023878178b317e559b97d7695246374bb1 (diff) | |
parent | faa7d8a3f7cba02eca7e247108a6b98ea9daf373 (diff) |
Merge bitcoin/bitcoin#24224: util: Add SaturatingAdd helper
faa7d8a3f7cba02eca7e247108a6b98ea9daf373 util: Add SaturatingAdd helper (MarcoFalke)
Pull request description:
Seems good to have this in the repo, as it might be needed to write cleaner code. For example:
* https://github.com/bitcoin/bitcoin/pull/24090#issuecomment-1019948200
* https://github.com/bitcoin/bitcoin/pull/23418#discussion_r744953272
* ...
ACKs for top commit:
MarcoFalke:
Added a test. Should be trivial to re-ACK with `git range-diff bitcoin-core/master fa90189cbf faa7d8a3f7`
klementtan:
reACK faa7d8a3f7
vasild:
ACK faa7d8a3f7cba02eca7e247108a6b98ea9daf373
Tree-SHA512: d0e6efdba7dfcbdd16ab4539a7f5e45a97d17792e42586c3c52caaae3fc70612dc9e364359658de5de5718fb8c2a765a59ceb2230098355394fa067a9732bc2a
Diffstat (limited to 'src/test/fuzz')
-rw-r--r-- | src/test/fuzz/addition_overflow.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/test/fuzz/addition_overflow.cpp b/src/test/fuzz/addition_overflow.cpp index cfad41659e..372c1a370e 100644 --- a/src/test/fuzz/addition_overflow.cpp +++ b/src/test/fuzz/addition_overflow.cpp @@ -26,6 +26,12 @@ void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider) const T i = fuzzed_data_provider.ConsumeIntegral<T>(); const T j = fuzzed_data_provider.ConsumeIntegral<T>(); const bool is_addition_overflow_custom = AdditionOverflow(i, j); + const auto maybe_add{CheckedAdd(i, j)}; + const auto sat_add{SaturatingAdd(i, j)}; + assert(is_addition_overflow_custom == !maybe_add.has_value()); + assert(is_addition_overflow_custom == AdditionOverflow(j, i)); + assert(maybe_add == CheckedAdd(j, i)); + assert(sat_add == SaturatingAdd(j, i)); #if defined(HAVE_BUILTIN_ADD_OVERFLOW) T result_builtin; const bool is_addition_overflow_builtin = __builtin_add_overflow(i, j, &result_builtin); @@ -33,11 +39,14 @@ void TestAdditionOverflow(FuzzedDataProvider& fuzzed_data_provider) if (!is_addition_overflow_custom) { assert(i + j == result_builtin); } -#else - if (!is_addition_overflow_custom) { - (void)(i + j); - } #endif + if (is_addition_overflow_custom) { + assert(sat_add == std::numeric_limits<T>::min() || sat_add == std::numeric_limits<T>::max()); + } else { + const auto add{i + j}; + assert(add == maybe_add.value()); + assert(add == sat_add); + } } } // namespace |