Skip to content

Commit

Permalink
[FOLD] Prevent conversion from STNumber* to Number*.
Browse files Browse the repository at this point in the history
* This prevents code trying to delete a STNumber via a
  pointer to the base class using a non-virtual destructor.
  • Loading branch information
HowardHinnant committed Oct 24, 2024
1 parent b74bdb0 commit 05ecfc6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions include/xrpl/protocol/STNumber.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ namespace ripple {
* without paying the storage cost of duplicating asset information
* that may be deduced from the context.
*/
class STNumber : public STBase, public CountedObject<STNumber>, public Number
class STNumber : public STBase, public CountedObject<STNumber>
{
Number value_;

public:
using value_type = Number;

STNumber() = default;
explicit STNumber(SField const& field, Number const& value = Number());
STNumber(SerialIter& sit, SField const& field);

using Number::operator=;

SerializedTypeID
getSType() const override;
std::string
Expand All @@ -68,6 +68,8 @@ class STNumber : public STBase, public CountedObject<STNumber>, public Number
bool
isDefault() const override;

operator Number() const {return value_;}

private:
STBase*
copy(std::size_t n, void* buf) const override;
Expand Down
22 changes: 11 additions & 11 deletions src/libxrpl/protocol/STNumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace ripple {

STNumber::STNumber(SField const& field, Number const& value)
: STBase(field), Number(value)
: STBase(field), value_(value)
{
}

Expand All @@ -34,7 +34,7 @@ STNumber::STNumber(SerialIter& sit, SField const& field) : STBase(field)
// to guarantee their order of execution.
auto mantissa = sit.geti64();
auto exponent = sit.geti32();
*this = Number{mantissa, exponent};
value_ = Number{mantissa, exponent};
}

SerializedTypeID
Expand All @@ -46,34 +46,34 @@ STNumber::getSType() const
std::string
STNumber::getText() const
{
return to_string(*this);
return to_string(value_);
}

Json::Value
STNumber::getJson(JsonOptions) const
{
return to_string(*this);
return to_string(value_);
}

void
STNumber::add(Serializer& s) const
{
assert(getFName().isBinary());
assert(getFName().fieldType == getSType());
s.add64(this->mantissa());
s.add32(this->exponent());
s.add64(value_.mantissa());
s.add32(value_.exponent());
}

Number const&
STNumber::value() const
{
return *this;
return value_;
}

void
STNumber::setValue(Number const& v)
{
*this = v;
value_ = v;
}

STBase*
Expand All @@ -92,14 +92,14 @@ bool
STNumber::isEquivalent(STBase const& t) const
{
assert(t.getSType() == this->getSType());
Number const& v = dynamic_cast<Number const&>(t);
return *this == v;
STNumber const& v = dynamic_cast<STNumber const&>(t);
return value_ == v;
}

bool
STNumber::isDefault() const
{
return *this == Number();
return value_ == Number();
}

std::ostream&
Expand Down
2 changes: 2 additions & 0 deletions src/test/protocol/STNumber_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ struct STNumber_test : public beast::unit_test::suite
void
run() override
{
static_assert(!std::is_convertible_v<STNumber*, Number*>);

{
STNumber const stnum{sfNumber};
BEAST_EXPECT(stnum.getSType() == STI_NUMBER);
Expand Down

0 comments on commit 05ecfc6

Please sign in to comment.