refactor: change the return type of mulDiv to std::optional (#4243)

- Previously, mulDiv had `std::pair<bool, uint64_t>` as the output type.
  - This is an error-prone interface as it is easy to ignore when
    overflow occurs.
- Using a return type of `std::optional` should decrease the likelihood
  of ignoring overflow.
  - It also allows for the use of optional::value_or() as a way to
    explicitly recover from overflow.
- Include limits.h header file preprocessing directive in order to
  satisfy gcc's numeric_limits incomplete_type requirement.

Fix #3495

---------

Co-authored-by: John Freeman <jfreeman08@gmail.com>
This commit is contained in:
Chenna Keshava B S
2023-07-05 20:11:19 -07:00
committed by GitHub
parent 77dc63b549
commit c6fee28b92
12 changed files with 93 additions and 83 deletions

View File

@@ -20,7 +20,6 @@
#include <ripple/app/ledger/OpenLedger.h>
#include <ripple/app/main/Application.h>
#include <ripple/app/misc/TxQ.h>
#include <ripple/basics/mulDiv.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/Feature.h>
#include <ripple/rpc/Context.h>

View File

@@ -738,9 +738,9 @@ checkFee(
auto const limit = [&]() {
// Scale fee units to drops:
auto const result = mulDiv(feeDefault, mult, div);
if (!result.first)
if (!result)
Throw<std::overflow_error>("mulDiv");
return result.second;
return *result;
}();
if (fee > limit)