mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Add safe_cast (RIPD-1702):
This change ensures that no overflow can occur when casting between enums and integral types.
This commit is contained in:
committed by
Nik Bougalis
parent
494724578a
commit
148bbf4e8f
@@ -46,6 +46,7 @@
|
||||
#include <ripple/app/tx/apply.h>
|
||||
#include <ripple/basics/ByteUtilities.h>
|
||||
#include <ripple/basics/ResolverAsio.h>
|
||||
#include <ripple/basics/safe_cast.h>
|
||||
#include <ripple/basics/Sustain.h>
|
||||
#include <ripple/basics/PerfLog.h>
|
||||
#include <ripple/json/json_reader.h>
|
||||
@@ -1068,7 +1069,7 @@ public:
|
||||
*db << "PRAGMA page_count;", soci::into(pageCount);
|
||||
std::uint32_t freePages = maxPages - pageCount;
|
||||
std::uint64_t freeSpace =
|
||||
static_cast<std::uint64_t>(freePages) * pageSize;
|
||||
safe_cast<std::uint64_t>(freePages) * pageSize;
|
||||
JLOG(m_journal.info())
|
||||
<< "Transaction DB pathname: " << dbPath.string()
|
||||
<< "; file size: " << dbSize.value_or(-1) << " bytes"
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
#include <ripple/basics/base64.h>
|
||||
#include <ripple/basics/mulDiv.h>
|
||||
#include <ripple/basics/PerfLog.h>
|
||||
#include <ripple/basics/safe_cast.h>
|
||||
#include <ripple/basics/UptimeClock.h>
|
||||
#include <ripple/core/ConfigSections.h>
|
||||
#include <ripple/crypto/csprng.h>
|
||||
@@ -1664,7 +1665,7 @@ void NetworkOPsImp::pubServer ()
|
||||
if(f.em)
|
||||
{
|
||||
auto const loadFactor =
|
||||
std::max(static_cast<std::uint64_t>(f.loadFactorServer),
|
||||
std::max(safe_cast<std::uint64_t>(f.loadFactorServer),
|
||||
mulDiv(f.em->openLedgerFeeLevel, f.loadBaseServer,
|
||||
f.em->referenceFeeLevel).second);
|
||||
|
||||
@@ -2121,7 +2122,7 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters)
|
||||
{
|
||||
if (when)
|
||||
info[jss::validator_list_expires] =
|
||||
static_cast<Json::UInt>(when->time_since_epoch().count());
|
||||
safe_cast<Json::UInt>(when->time_since_epoch().count());
|
||||
else
|
||||
info[jss::validator_list_expires] = 0;
|
||||
}
|
||||
@@ -2227,7 +2228,7 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters)
|
||||
auto const loadBaseFeeEscalation =
|
||||
escalationMetrics.referenceFeeLevel;
|
||||
|
||||
auto const loadFactor = std::max(static_cast<std::uint64_t>(loadFactorServer),
|
||||
auto const loadFactor = std::max(safe_cast<std::uint64_t>(loadFactorServer),
|
||||
mulDiv(loadFactorFeeEscalation, loadBaseServer, loadBaseFeeEscalation).second);
|
||||
|
||||
if (!human)
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <ripple/app/misc/LoadFeeTrack.h>
|
||||
#include <ripple/basics/contract.h>
|
||||
#include <ripple/basics/Log.h>
|
||||
#include <ripple/basics/safe_cast.h>
|
||||
#include <ripple/core/Config.h>
|
||||
#include <ripple/ledger/ReadView.h>
|
||||
#include <ripple/protocol/STAmount.h>
|
||||
@@ -149,8 +150,8 @@ scaleFeeLoad(std::uint64_t fee, LoadFeeTrack const& feeTrack,
|
||||
// The denominator of the fraction we're trying to compute.
|
||||
// fees.units and lftNormalFee are both 32 bit,
|
||||
// so the multiplication can't overflow.
|
||||
auto den = static_cast<std::uint64_t>(fees.units)
|
||||
* static_cast<std::uint64_t>(feeTrack.getLoadBase());
|
||||
auto den = safe_cast<std::uint64_t>(fees.units)
|
||||
* safe_cast<std::uint64_t>(feeTrack.getLoadBase());
|
||||
// Reduce fee * baseFee * feeFactor / (fees.units * lftNormalFee)
|
||||
// to lowest terms.
|
||||
lowestTerms(fee, den);
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <ripple/app/misc/HashRouter.h>
|
||||
#include <ripple/basics/chrono.h>
|
||||
#include <ripple/basics/Log.h>
|
||||
#include <ripple/basics/safe_cast.h>
|
||||
#include <ripple/conditions/Condition.h>
|
||||
#include <ripple/conditions/Fulfillment.h>
|
||||
#include <ripple/ledger/ApplyView.h>
|
||||
@@ -358,7 +359,7 @@ EscrowFinish::calculateBaseFee (
|
||||
if (auto const fb = tx[~sfFulfillment])
|
||||
{
|
||||
extraFee += view.fees().units *
|
||||
(32 + static_cast<std::uint64_t> (fb->size() / 16));
|
||||
(32 + safe_cast<std::uint64_t> (fb->size() / 16));
|
||||
}
|
||||
|
||||
return Transactor::calculateBaseFee (view, tx) + extraFee;
|
||||
|
||||
Reference in New Issue
Block a user