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
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <ripple/basics/contract.h>
|
||||
#include <ripple/basics/Log.h>
|
||||
#include <ripple/basics/safe_cast.h>
|
||||
#include <ripple/json/to_string.h>
|
||||
#include <ripple/protocol/Indexes.h>
|
||||
#include <ripple/protocol/JsonFields.h>
|
||||
@@ -32,6 +33,10 @@ STLedgerEntry::STLedgerEntry (Keylet const& k)
|
||||
, key_ (k.key)
|
||||
, type_ (k.type)
|
||||
{
|
||||
if (!(0u <= type_ &&
|
||||
type_ <= std::min<unsigned>(std::numeric_limits<std::uint16_t>::max(),
|
||||
std::numeric_limits<std::underlying_type_t<LedgerEntryType>>::max())))
|
||||
Throw<std::runtime_error> ("invalid ledger entry type: out of range");
|
||||
auto const format =
|
||||
LedgerFormats::getInstance().findByType (type_);
|
||||
|
||||
@@ -66,7 +71,7 @@ STLedgerEntry::STLedgerEntry (
|
||||
void STLedgerEntry::setSLEType ()
|
||||
{
|
||||
auto format = LedgerFormats::getInstance().findByType (
|
||||
static_cast <LedgerEntryType> (
|
||||
safe_cast <LedgerEntryType> (
|
||||
getFieldU16 (sfLedgerEntryType)));
|
||||
|
||||
if (format == nullptr)
|
||||
|
||||
Reference in New Issue
Block a user