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:
Howard Hinnant
2018-12-21 17:13:58 -05:00
committed by Nik Bougalis
parent 494724578a
commit 148bbf4e8f
35 changed files with 213 additions and 86 deletions

View File

@@ -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)