Skip to content

Commit

Permalink
[fold] Bring back safe case & misc reviews changes
Browse files Browse the repository at this point in the history
  • Loading branch information
seelabs committed Jan 12, 2024
1 parent 1da9ea7 commit a7d5ed6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/ripple/protocol/impl/b58_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ inplace_bigint_mul(std::span<std::uint64_t> a, std::uint64_t b)
[[nodiscard]] inline std::uint64_t
inplace_bigint_div_rem(std::span<uint64_t> numerator, std::uint64_t divisor)
{
if (numerator.size() == 0)
{
// should never happen, but if it does then it seems natural to define
// the a null set of numbers to be zero, so the remainder is also zero.
assert(0);
return 0;
}

auto to_u128 = [](std::uint64_t high,
std::uint64_t low) -> unsigned __int128 {
unsigned __int128 const high128 = high;
Expand All @@ -134,11 +142,13 @@ inplace_bigint_div_rem(std::span<uint64_t> numerator, std::uint64_t divisor)
unsigned __int128 const denom128 = denom;
unsigned __int128 const d = num / denom128;
unsigned __int128 const r = num - (denom128 * d);
assert(d >> 64 == 0);
assert(r >> 64 == 0);
return {static_cast<std::uint64_t>(d), static_cast<std::uint64_t>(r)};
};

std::uint64_t prev_rem = 0;
std::size_t const last_index = numerator.size() - 1;
int const last_index = numerator.size() - 1;
std::tie(numerator[last_index], prev_rem) =
div_rem(numerator[last_index], divisor);
for (int i = last_index - 1; i >= 0; --i)
Expand Down
5 changes: 3 additions & 2 deletions src/ripple/protocol/impl/tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <ripple/protocol/tokens.h>

#include <ripple/basics/safe_cast.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/impl/b58_utils.h>

Expand Down Expand Up @@ -329,7 +330,7 @@ encodeBase58Token(TokenType type, void const* token, std::size_t size)

// Lay the data out as
// <type><token><checksum>
buf[0] = static_cast<std::underlying_type_t<TokenType>>(type);
buf[0] = safe_cast<std::underlying_type_t<TokenType>>(type);
if (size)
std::memcpy(buf.data() + 1, token, size);
checksum(buf.data() + 1 + size, buf.data(), 1 + size);
Expand All @@ -348,7 +349,7 @@ decodeBase58Token(std::string const& s, TokenType type)
return {};

// The type must match.
if (type != static_cast<TokenType>(static_cast<std::uint8_t>(ret[0])))
if (type != safe_cast<TokenType>(static_cast<std::uint8_t>(ret[0])))
return {};

// And the checksum must as well.
Expand Down

0 comments on commit a7d5ed6

Please sign in to comment.