Skip to content

Commit

Permalink
Fix dangling reference returns
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Oct 25, 2024
1 parent e246aa4 commit ffd4659
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 49 deletions.
10 changes: 5 additions & 5 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ std::optional<nano::account> nano::send_block::destination_field () const
return hashables.destination;
}

nano::root const & nano::send_block::root () const
nano::root nano::send_block::root () const
{
return hashables.previous;
}
Expand Down Expand Up @@ -899,7 +899,7 @@ std::optional<nano::block_hash> nano::open_block::source_field () const
return hashables.source;
}

nano::root const & nano::open_block::root () const
nano::root nano::open_block::root () const
{
return hashables.account;
}
Expand Down Expand Up @@ -1165,7 +1165,7 @@ bool nano::change_block::valid_predecessor (nano::block const & block_a) const
return result;
}

nano::root const & nano::change_block::root () const
nano::root nano::change_block::root () const
{
return hashables.previous;
}
Expand Down Expand Up @@ -1482,7 +1482,7 @@ bool nano::state_block::valid_predecessor (nano::block const & block_a) const
return true;
}

nano::root const & nano::state_block::root () const
nano::root nano::state_block::root () const
{
if (!hashables.previous.is_zero ())
{
Expand Down Expand Up @@ -1836,7 +1836,7 @@ std::optional<nano::block_hash> nano::receive_block::source_field () const
return hashables.source;
}

nano::root const & nano::receive_block::root () const
nano::root nano::receive_block::root () const
{
return hashables.previous;
}
Expand Down
12 changes: 6 additions & 6 deletions nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class block
virtual uint64_t block_work () const = 0;
virtual void block_work_set (uint64_t) = 0;
// Previous block or account number for open blocks
virtual nano::root const & root () const = 0;
virtual nano::root root () const = 0;
// Qualified root value based on previous() and root()
virtual nano::qualified_root qualified_root () const;
virtual void serialize (nano::stream &) const = 0;
Expand Down Expand Up @@ -123,7 +123,7 @@ class send_block : public nano::block
virtual ~send_block () = default;
uint64_t block_work () const override;
void block_work_set (uint64_t) override;
nano::root const & root () const override;
nano::root root () const override;
void serialize (nano::stream &) const override;
bool deserialize (nano::stream &);
void serialize_json (std::string &, bool = false) const override;
Expand Down Expand Up @@ -177,7 +177,7 @@ class receive_block : public nano::block
virtual ~receive_block () = default;
uint64_t block_work () const override;
void block_work_set (uint64_t) override;
nano::root const & root () const override;
nano::root root () const override;
void serialize (nano::stream &) const override;
bool deserialize (nano::stream &);
void serialize_json (std::string &, bool = false) const override;
Expand Down Expand Up @@ -232,7 +232,7 @@ class open_block : public nano::block
virtual ~open_block () = default;
uint64_t block_work () const override;
void block_work_set (uint64_t) override;
nano::root const & root () const override;
nano::root root () const override;
void serialize (nano::stream &) const override;
bool deserialize (nano::stream &);
void serialize_json (std::string &, bool = false) const override;
Expand Down Expand Up @@ -287,7 +287,7 @@ class change_block : public nano::block
virtual ~change_block () = default;
uint64_t block_work () const override;
void block_work_set (uint64_t) override;
nano::root const & root () const override;
nano::root root () const override;
void serialize (nano::stream &) const override;
bool deserialize (nano::stream &);
void serialize_json (std::string &, bool = false) const override;
Expand Down Expand Up @@ -353,7 +353,7 @@ class state_block : public nano::block
virtual ~state_block () = default;
uint64_t block_work () const override;
void block_work_set (uint64_t) override;
nano::root const & root () const override;
nano::root root () const override;
void serialize (nano::stream &) const override;
bool deserialize (nano::stream &);
void serialize_json (std::string &, bool = false) const override;
Expand Down
7 changes: 7 additions & 0 deletions nano/lib/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ std::ostream & nano::operator<< (std::ostream & os, const uint512_union & val)
return os;
}

std::ostream & nano::operator<< (std::ostream & os, const hash_or_account & val)
{
// TODO: Replace with streaming implementation
os << val.to_string ();
return os;
}

#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4146) // warning C4146: unary minus operator applied to unsigned type, result still unsigned
Expand Down
52 changes: 17 additions & 35 deletions nano/lib/numbers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ class uint256_union
};
static_assert (std::is_nothrow_move_constructible<uint256_union>::value, "uint256_union should be noexcept MoveConstructible");

class link;
class root;
class hash_or_account;

// All keys and hashes are 256 bit.
class block_hash final : public uint256_union
{
Expand All @@ -212,18 +208,6 @@ class block_hash final : public uint256_union
{
return number ();
}
operator nano::link () const
{
return nano::link{ *this };
}
operator nano::root () const
{
return nano::root{ *this };
}
operator nano::hash_or_account () const
{
return nano::hash_or_account{ *this };
}
};

class public_key final : public uint256_union
Expand Down Expand Up @@ -260,18 +244,6 @@ class public_key final : public uint256_union
{
return number ();
}
operator nano::link () const
{
return nano::link{ *this };
}
operator nano::root () const
{
return nano::root{ *this };
}
operator nano::hash_or_account () const
{
return nano::hash_or_account{ *this };
}
};

class wallet_id : public uint256_union
Expand All @@ -289,7 +261,7 @@ class hash_or_account
account{} {};
hash_or_account (uint64_t value) :
raw{ value } {};
explicit hash_or_account (uint256_union const & value) :
hash_or_account (uint256_union const & value) :
raw{ value } {};

void clear ()
Expand Down Expand Up @@ -325,11 +297,11 @@ class hash_or_account
{
return *this <=> other == 0;
}
operator nano::uint256_t () const
explicit operator nano::uint256_t () const
{
return raw.number ();
}
operator nano::uint256_union () const
explicit operator nano::uint256_union () const
{
return raw;
}
Expand Down Expand Up @@ -399,13 +371,13 @@ class uint512_union
{
public:
uint512_union () = default;
uint512_union (nano::uint256_union const & upper, nano::uint256_union const & lower) :
uint256s{ upper, lower } {};
uint512_union (nano::uint512_t const & value)
{
bytes.fill (0);
boost::multiprecision::export_bits (value, bytes.rbegin (), 8, false);
}
uint512_union (nano::uint256_union const & upper, nano::uint256_union const & lower) :
uint256s{ upper, lower } {};

nano::uint512_union & operator^= (nano::uint512_union const & other)
{
Expand Down Expand Up @@ -473,7 +445,11 @@ class signature : public uint512_union
class qualified_root : public uint512_union
{
public:
using uint512_union::uint512_union;
qualified_root () = default;
qualified_root (nano::root const & root, nano::block_hash const & previous) :
uint512_union{ root.as_union (), previous.as_union () } {};
qualified_root (nano::uint512_t const & value) :
uint512_union{ value } {};

nano::root root () const
{
Expand Down Expand Up @@ -501,6 +477,7 @@ bool from_string_hex (std::string const &, uint64_t &);
std::ostream & operator<< (std::ostream &, const uint128_union &);
std::ostream & operator<< (std::ostream &, const uint256_union &);
std::ostream & operator<< (std::ostream &, const uint512_union &);
std::ostream & operator<< (std::ostream &, const hash_or_account &);

/**
* Convert a double to string in fixed format
Expand Down Expand Up @@ -643,6 +620,11 @@ struct fmt::formatter<nano::uint512_union> : fmt::ostream_formatter
{
};

template <>
struct fmt::formatter<nano::hash_or_account> : fmt::ostream_formatter
{
};

template <>
struct fmt::formatter<nano::block_hash> : fmt::formatter<nano::uint256_union>
{
Expand All @@ -656,4 +638,4 @@ struct fmt::formatter<nano::public_key> : fmt::formatter<nano::uint256_union>
template <>
struct fmt::formatter<nano::qualified_root> : fmt::formatter<nano::uint512_union>
{
};
};
6 changes: 3 additions & 3 deletions nano/node/bootstrap/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void nano::pulls_cache::add (nano::pull_info const & pull_a)
cache.erase (cache.begin ());
}
debug_assert (cache.size () <= cache_size_max);
nano::uint512_union head_512 (pull_a.account_or_head, pull_a.head_original);
nano::uint512_union head_512 (pull_a.account_or_head.as_union (), pull_a.head_original);
auto existing (cache.get<account_head_tag> ().find (head_512));
if (existing == cache.get<account_head_tag> ().end ())
{
Expand All @@ -336,7 +336,7 @@ void nano::pulls_cache::add (nano::pull_info const & pull_a)
void nano::pulls_cache::update_pull (nano::pull_info & pull_a)
{
nano::lock_guard<nano::mutex> guard{ pulls_cache_mutex };
nano::uint512_union head_512 (pull_a.account_or_head, pull_a.head_original);
nano::uint512_union head_512 (pull_a.account_or_head.as_union (), pull_a.head_original);
auto existing (cache.get<account_head_tag> ().find (head_512));
if (existing != cache.get<account_head_tag> ().end ())
{
Expand All @@ -347,7 +347,7 @@ void nano::pulls_cache::update_pull (nano::pull_info & pull_a)
void nano::pulls_cache::remove (nano::pull_info const & pull_a)
{
nano::lock_guard<nano::mutex> guard{ pulls_cache_mutex };
nano::uint512_union head_512 (pull_a.account_or_head, pull_a.head_original);
nano::uint512_union head_512 (pull_a.account_or_head.as_union (), pull_a.head_original);
cache.get<account_head_tag> ().erase (head_512);
}

Expand Down

0 comments on commit ffd4659

Please sign in to comment.