8000 Fix: XRPAmount Overflow by dangell7 · Pull Request #5330 · XRPLF/rippled · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix: XRPAmount Overflow #5330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 56 additions & 8 deletions include/xrpl/protocol/XRPAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include <boost/operators.hpp>

#include <cstdint>
#include <limits>
#include <optional>
#include <stdexcept>
#include <string>
#include <type_traits>

Expand All @@ -48,6 +50,51 @@ class XRPAmount : private boost::totally_ordered<XRPAmount>,
private:
value_type drops_;

static constexpr value_type
safeAdd(value_type a, value_type b)
{
if ((b > 0 && a > std::numeric_limits<value_type>::max() - b) ||
(b < 0 && a < std::numeric_limits<value_type>::min() - b))
throw std::overflow_error("XRPAmount overflow");
return a + b;
}

static constexpr value_type
safeSub(value_type a, value_type b)
{
if (b == std::numeric_limits<value_type>::min())
throw std::underflow_error("XRPAmount underflow");
return safeAdd(a, -b);
}

static constexpr value_type
safeMul(value_type a, value_type b)
{
if (a == 0 || b == 0)
return 0;

if ((a > 0 && b > 0 &&
a > std::numeric_limits<value_type>::max() / b) ||
(a > 0 && b < 0 &&
b < std::numeric_limits<value_type>::min() / a) ||
(a < 0 && b > 0 &&
a < std::numeric_limits<value_type>::min() / b) ||
(a < 0 && b < 0 && a < std::numeric_limits<value_type>::max() / b))
{
throw std::overflow_error("XRPAmount overflow");
}

return a * b;
}

static constexpr value_type
safeNeg(value_type a)
{
if (a == std::numeric_limits<value_type>::min())
throw std::overflow_error("XRPAmount overflow");
return -a;
}

public:
XRPAmount() = default;
constexpr XRPAmount(XRPAmount const& other) = default;
Expand Down Expand Up @@ -81,10 +128,11 @@ class XRPAmount : private boost::totally_ordered<XRPAmount>,
return *this;
}

// Use safe multiplication
constexpr XRPAmount
operator*(value_type const& rhs) const
{
return XRPAmount{drops_ * rhs};
return XRPAmount{safeMul(drops_, rhs)};
}

friend constexpr XRPAmount
Expand All @@ -97,42 +145,42 @@ class XRPAmount : private boost::totally_ordered<XRPAmount>,
XRPAmount&
operator+=(XRPAmount const& other)
{
drops_ += other.drops();
drops_ = safeAdd(drops_, other.drops());
return *this;
}

XRPAmount&
operator-=(XRPAmount const& other)
{
drops_ -= other.drops();
drops_ = safeSub(drops_, other.drops());
return *this;
}

XRPAmount&
operator+=(value_type const& rhs)
{
drops_ += rhs;
drops_ = safeAdd(drops_, rhs);
return *this;
}

XRPAmount&
operator-=(value_type const& rhs)
{
drops_ -= rhs;
drops_ = safeSub(drops_, rhs);
return *this;
}

XRPAmount&
operator*=(value_type const& rhs)
{
drops_ *= rhs;
drops_ = safeMul(drops_, rhs);
return *this;
}

XRPAmount
operator-() const
{
return XRPAmount{-drops_};
return XRPAmount{safeNeg(drops_)};
}

bool
Expand Down Expand Up @@ -308,4 +356,4 @@ mulRatio(

} // namespace ripple

#endif // RIPPLE_BASICS_XRPAMOUNT_H_INCLUDED
#endif // RIPPLE_PROTOCOL_XRPAMOUNT_H_INCLUDED
Loading
0