Skip to content

Commit

Permalink
Give names to asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Jul 19, 2024
1 parent 6767610 commit 8d7cd9c
Show file tree
Hide file tree
Showing 241 changed files with 2,521 additions and 984 deletions.
3 changes: 2 additions & 1 deletion include/xrpl/basics/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ class Buffer
{
// Ensure the slice isn't a subset of the buffer.
XRPL_ASSERT(
"ripple::Buffer::operator=(Slice) : input not a subset",
s.size() == 0 || size_ == 0 || s.data() < p_.get() ||
s.data() >= p_.get() + size_);
s.data() >= p_.get() + size_);

if (auto p = alloc(s.size()))
std::memcpy(p, s.data(), s.size());
Expand Down
10 changes: 7 additions & 3 deletions include/xrpl/basics/FeeUnits.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,13 @@ mulDivU(Source1 value, Dest mul, Source2 div)
{
// split the asserts so if one hits, the user can tell which
// without a debugger.
XRPL_ASSERT(value.value() >= 0);
XRPL_ASSERT(mul.value() >= 0);
XRPL_ASSERT(div.value() >= 0);
XRPL_ASSERT(
"ripple::feeunit::mulDivU : minimum value input",
value.value() >= 0);
XRPL_ASSERT(
"ripple::feeunit::mulDivU : minimum mul input", mul.value() >= 0);
XRPL_ASSERT(
"ripple::feeunit::mulDivU : minimum div input", div.value() >= 0);
return std::nullopt;
}

Expand Down
14 changes: 11 additions & 3 deletions include/xrpl/basics/SlabAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef RIPPLE_BASICS_SLABALLOCATOR_H_INCLUDED
#define RIPPLE_BASICS_SLABALLOCATOR_H_INCLUDED

#include <xrpl/basics/ByteUtilities.h>
#include <xrpl/beast/type_name.h>
#include <xrpl/beast/utility/instrumentation.h>

Expand All @@ -32,6 +33,7 @@
#include <cstdint>
#include <cstring>
#include <mutex>
#include <vector>

#if BOOST_OS_LINUX
#include <sys/mman.h>
Expand Down Expand Up @@ -141,7 +143,9 @@ class SlabAllocator
void
deallocate(std::uint8_t* ptr) noexcept
{
XRPL_ASSERT(own(ptr));
XRPL_ASSERT(
"ripple::SlabAllocator::SlabBlock::deallocate : own input",
own(ptr));

std::lock_guard l(m_);

Expand Down Expand Up @@ -184,7 +188,9 @@ class SlabAllocator
boost::alignment::align_up(sizeof(Type) + extra, itemAlignment_))
, slabSize_(alloc)
{
XRPL_ASSERT((itemAlignment_ & (itemAlignment_ - 1)) == 0);
XRPL_ASSERT(
"ripple::SlabAllocator::SlabAllocator : valid alignment",
(itemAlignment_ & (itemAlignment_ - 1)) == 0);
}

SlabAllocator(SlabAllocator const& other) = delete;
Expand Down Expand Up @@ -294,7 +300,9 @@ class SlabAllocator
bool
deallocate(std::uint8_t* ptr) noexcept
{
XRPL_ASSERT(ptr);
XRPL_ASSERT(
"ripple::SlabAllocator::SlabAllocator::deallocate : non-null input",
ptr);

for (auto slab = slabs_.load(); slab != nullptr; slab = slab->next_)
{
Expand Down
4 changes: 3 additions & 1 deletion include/xrpl/basics/Slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ class Slice
std::uint8_t
operator[](std::size_t i) const noexcept
{
XRPL_ASSERT(i < size_);
XRPL_ASSERT(
"ripple::Slice::operator[](std::size_t) const : input within range",
i < size_);
return data_[i];
}

Expand Down
2 changes: 2 additions & 0 deletions include/xrpl/basics/base_uint.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class base_uint
explicit base_uint(Container const& c)
{
XRPL_ASSERT(
"ripple::base_uint::base_uint(Container auto) : input size match",
c.size() * sizeof(typename Container::value_type) == size());
std::memcpy(data_.data(), c.data(), size());
}
Expand All @@ -303,6 +304,7 @@ class base_uint
operator=(Container const& c)
{
XRPL_ASSERT(
"ripple::base_uint::operator=(Container auto) : input size match",
c.size() * sizeof(typename Container::value_type) == size());
std::memcpy(data_.data(), c.data(), size());
return *this;
Expand Down
5 changes: 4 additions & 1 deletion include/xrpl/basics/partitioned_unordered_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ class partitioned_unordered_map
? *partitions
: std::thread::hardware_concurrency();
map_.resize(partitions_);
XRPL_ASSERT(partitions_);
XRPL_ASSERT(
"ripple::partitioned_unordered_map::partitioned_unordered_map : "
"nonzero partitions",
partitions_);
}

std::size_t
Expand Down
2 changes: 1 addition & 1 deletion include/xrpl/basics/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ std::enable_if_t<
Integral>
rand_int(Engine& engine, Integral min, Integral max)
{
XRPL_ASSERT(max > min);
XRPL_ASSERT("ripple::rand_int : max over min inputs", max > min);

// This should have no state and constructing it should
// be very cheap. If that turns out not to be the case
Expand Down
4 changes: 3 additions & 1 deletion include/xrpl/basics/spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ class packed_spinlock
packed_spinlock(std::atomic<T>& lock, int index)
: bits_(lock), mask_(static_cast<T>(1) << index)
{
XRPL_ASSERT(index >= 0 && (mask_ != 0));
XRPL_ASSERT(
"ripple::packed_spinlock::packed_spinlock : valid index and mask",
index >= 0 && (mask_ != 0));
}

[[nodiscard]] bool
Expand Down
10 changes: 8 additions & 2 deletions include/xrpl/beast/asio/io_latency_probe.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ class io_latency_probe
, m_repeat(repeat)
, m_probe(probe)
{
XRPL_ASSERT(m_probe);
XRPL_ASSERT(
"beast::io_latency_probe::sample_op::sample_op : non-null "
"probe input",
m_probe);
m_probe->addref();
}

Expand All @@ -184,7 +187,10 @@ class io_latency_probe
, m_repeat(from.m_repeat)
, m_probe(from.m_probe)
{
XRPL_ASSERT(m_probe);
XRPL_ASSERT(
"beast::io_latency_probe::sample_op::sample_op(sample_op&&) : "
"non-null probe input",
m_probe);
from.m_probe = nullptr;
}

Expand Down
8 changes: 6 additions & 2 deletions include/xrpl/beast/clock/manual_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class manual_clock : public abstract_clock<Clock>
void
set(time_point const& when)
{
XRPL_ASSERT(!Clock::is_steady || when >= now_);
XRPL_ASSERT(
"beast::manual_clock::set(time_point) : forward input",
!Clock::is_steady || when >= now_);
now_ = when;
}

Expand All @@ -78,7 +80,9 @@ class manual_clock : public abstract_clock<Clock>
void
advance(std::chrono::duration<Rep, Period> const& elapsed)
{
XRPL_ASSERT(!Clock::is_steady || (now_ + elapsed) >= now_);
XRPL_ASSERT(
"beast::manual_clock::advance(duration) : forward input",
!Clock::is_steady || (now_ + elapsed) >= now_);
now_ += elapsed;
}

Expand Down
10 changes: 8 additions & 2 deletions include/xrpl/beast/container/detail/aged_unordered_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,10 @@ class aged_unordered_container
size_type
bucket(Key const& k) const
{
XRPL_ASSERT(bucket_count() != 0);
XRPL_ASSERT(
"beast::detail::aged_unordered_container::bucket : nonzero bucket "
"count",
bucket_count() != 0);
return m_cont.bucket(k, std::cref(m_config.hash_function()));
}

Expand Down Expand Up @@ -1470,7 +1473,10 @@ class aged_unordered_container
{
if (would_exceed(additional))
m_buck.resize(size() + additional, m_cont);
XRPL_ASSERT(load_factor() <= max_load_factor());
XRPL_ASSERT(
"beast::detail::aged_unordered_container::maybe_rehash : maximum "
"load factor",
load_factor() <= max_load_factor());
}

// map, set
Expand Down
5 changes: 3 additions & 2 deletions include/xrpl/beast/core/LexicalCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ struct LexicalCast<Out, char const*>
bool
operator()(Out& out, char const* in) const
{
XRPL_ASSERT(in);
XRPL_ASSERT(
"beast::detail::LexicalCast(char const*) : non-null input", in);
return LexicalCast<Out, std::string_view>()(out, in);
}
};
Expand All @@ -175,7 +176,7 @@ struct LexicalCast<Out, char*>
bool
operator()(Out& out, char* in) const
{
XRPL_ASSERT(in);
XRPL_ASSERT("beast::detail::LexicalCast(char*) : non-null input", in);
return LexicalCast<Out, std::string_view>()(out, in);
}
};
Expand Down
2 changes: 1 addition & 1 deletion include/xrpl/beast/net/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ hash_append(Hasher& h, beast::IP::Address const& addr) noexcept
else if (addr.is_v6())
hash_append(h, addr.to_v6().to_bytes());
else
XRPL_UNREACHABLE();
XRPL_UNREACHABLE("beast::hash_append : invalid address type");

Check warning on line 99 in include/xrpl/beast/net/IPAddress.h

View check run for this annotation

Codecov / codecov/patch

include/xrpl/beast/net/IPAddress.h#L99

Added line #L99 was not covered by tests
}
} // namespace beast

Expand Down
4 changes: 3 additions & 1 deletion include/xrpl/beast/utility/Journal.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ class Journal
*/
Stream(Sink& sink, Severity level) : m_sink(sink), m_level(level)
{
XRPL_ASSERT(m_level < severities::kDisabled);
XRPL_ASSERT(
"beast::Journal::Stream::Stream : maximum level",
m_level < severities::kDisabled);
}

/** Construct or copy another Stream. */
Expand Down
6 changes: 3 additions & 3 deletions include/xrpl/beast/utility/instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#endif

#ifndef NDEBUG
#define XRPL_ASSERT(...) ALWAYS_OR_UNREACHABLE(((bool)(__VA_ARGS__)), "", {})
#define XRPL_UNREACHABLE() UNREACHABLE("", {})
#define XRPL_ASSERT(M, ...) ALWAYS_OR_UNREACHABLE(((bool)(__VA_ARGS__)), M, {})
#define XRPL_UNREACHABLE(M) UNREACHABLE(M, {})
#else
#define XRPL_ASSERT(...)
#define XRPL_UNREACHABLE()
#define XRPL_UNREACHABLE(M)
#endif

#endif
3 changes: 2 additions & 1 deletion include/xrpl/beast/utility/rngfill.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ rngfill(void* buffer, std::size_t bytes, Generator& g)
bytes -= sizeof(v);
}

XRPL_ASSERT(bytes < sizeof(result_type));
XRPL_ASSERT(
"beast::rngfill(void*) : maximum bytes", bytes < sizeof(result_type));

#ifdef __GNUC__
// gcc 11.1 (falsely) warns about an array-bounds overflow in release mode.
Expand Down
3 changes: 0 additions & 3 deletions include/xrpl/json/detail/json_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@

#include <xrpl/json/json_errors.h>

#define JSON_ASSERT_UNREACHABLE XRPL_UNREACHABLE()
#define JSON_ASSERT(condition) \
XRPL_ASSERT(condition); // @todo <= change this into an exception throw
#define JSON_ASSERT_MESSAGE(condition, message) \
if (!(condition)) \
ripple::Throw<Json::error>(message);
Expand Down
16 changes: 11 additions & 5 deletions include/xrpl/protocol/AmountConversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ toSTAmount(XRPAmount const& xrp)
inline STAmount
toSTAmount(XRPAmount const& xrp, Issue const& iss)
{
XRPL_ASSERT(isXRP(iss.account) && isXRP(iss.currency));
XRPL_ASSERT(
"ripple::toSTAmount : is XRP",
isXRP(iss.account) && isXRP(iss.currency));
return toSTAmount(xrp);
}

Expand All @@ -78,25 +80,29 @@ template <>
inline IOUAmount
toAmount<IOUAmount>(STAmount const& amt)
{
XRPL_ASSERT(amt.mantissa() < std::numeric_limits<std::int64_t>::max());
XRPL_ASSERT(
"ripple::toAmount<IOUAmount> : maximum mantissa",
amt.mantissa() < std::numeric_limits<std::int64_t>::max());
bool const isNeg = amt.negative();
std::int64_t const sMant =
isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();

XRPL_ASSERT(!isXRP(amt));
XRPL_ASSERT("ripple::toAmount<IOUAmount> : is not XRP", !isXRP(amt));
return IOUAmount(sMant, amt.exponent());
}

template <>
inline XRPAmount
toAmount<XRPAmount>(STAmount const& amt)
{
XRPL_ASSERT(amt.mantissa() < std::numeric_limits<std::int64_t>::max());
XRPL_ASSERT(
"ripple::toAmount<XRPAmount> : maximum mantissa",
amt.mantissa() < std::numeric_limits<std::int64_t>::max());
bool const isNeg = amt.negative();
std::int64_t const sMant =
isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa();

XRPL_ASSERT(isXRP(amt));
XRPL_ASSERT("ripple::toAmount<XRPAmount> : is XRP", isXRP(amt));
return XRPAmount(sMant);
}

Expand Down
14 changes: 11 additions & 3 deletions include/xrpl/protocol/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,30 @@ class FeatureBitset : private std::bitset<detail::numFeatures>

explicit FeatureBitset(base const& b) : base(b)
{
XRPL_ASSERT(b.count() == count());
XRPL_ASSERT(
"ripple::FeatureBitset::FeatureBitset(base) : count match",
b.count() == count());
}

template <class... Fs>
explicit FeatureBitset(uint256 const& f, Fs&&... fs)
{
initFromFeatures(f, std::forward<Fs>(fs)...);
XRPL_ASSERT(count() == (sizeof...(fs) + 1));
XRPL_ASSERT(
"ripple::FeatureBitset::FeatureBitset(uint256) : count and "
"sizeof... do match",
count() == (sizeof...(fs) + 1));
}

template <class Col>
explicit FeatureBitset(Col const& fs)
{
for (auto const& f : fs)
set(featureToBitsetIndex(f));
XRPL_ASSERT(fs.size() == count());
XRPL_ASSERT(
"ripple::FeatureBitset::FeatureBitset(Container auto) : count and "
"size do match",
fs.size() == count());
}

auto
Expand Down
3 changes: 2 additions & 1 deletion include/xrpl/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ page(uint256 const& root, std::uint64_t index = 0) noexcept;
inline Keylet
page(Keylet const& root, std::uint64_t index = 0) noexcept
{
XRPL_ASSERT(root.type == ltDIR_NODE);
XRPL_ASSERT(
"ripple::keylet::page : valid root type", root.type == ltDIR_NODE);
return page(root.key, index);
}
/** @} */
Expand Down
3 changes: 3 additions & 0 deletions include/xrpl/protocol/MultiApiJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ struct MultiApiJson
invoke_result_t<Fn, decltype(json.val[0]), Version, Args&&...>
{
XRPL_ASSERT(
"ripple::detail::MultiApiJson::operator<Args...>() : valid "
"version",
valid(version) && index(version) >= 0 && index(version) < size);
return std::invoke(
fn,
Expand All @@ -176,6 +178,7 @@ struct MultiApiJson
-> std::invoke_result_t<Fn, decltype(json.val[0])>
{
XRPL_ASSERT(
"ripple::detail::MultiApiJson::operator() : valid version",
valid(version) && index(version) >= 0 && index(version) < size);
return std::invoke(fn, json.val[index(version)]);
}
Expand Down
4 changes: 3 additions & 1 deletion include/xrpl/protocol/Quality.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ class Quality
friend double
relativeDistance(Quality const& q1, Quality const& q2)
{
XRPL_ASSERT(q1.m_value > 0 && q2.m_value > 0);
XRPL_ASSERT(
"ripple::Quality::relativeDistance : minimum inputs",
q1.m_value > 0 && q2.m_value > 0);

if (q1.m_value == q2.m_value) // make expected common case fast
return 0;
Expand Down
Loading

0 comments on commit 8d7cd9c

Please sign in to comment.