Skip to content

Commit

Permalink
refactor: Use operator== for equality tests of Node_format.
Browse files Browse the repository at this point in the history
It has padding bytes, so memcmp isn't necessarily safe. It is definitely
unsafe for fuzzed node formats.
  • Loading branch information
iphydf committed Jan 11, 2024
1 parent 9592d59 commit 2f31e05
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion toxcore/DHT_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Node_format random_node_format(const Random *rng)

bool operator==(Node_format const &a, Node_format const &b)
{
return std::memcmp(&a, &b, sizeof(Node_format)) == 0;
return std::memcmp(a.public_key, b.public_key, sizeof(a.public_key)) == 0
&& a.ip_port == b.ip_port;
}

std::ostream &operator<<(std::ostream &out, Node_format const &v)
Expand Down
24 changes: 24 additions & 0 deletions toxcore/network_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ IP_Port random_ip_port(const Random *rng)
return ip_port;
}

bool operator==(Family const &a, Family const &b) { return a.value == b.value; }

bool operator==(IP4 const &a, IP4 const &b) { return a.uint32 == b.uint32; }

bool operator==(IP6 const &a, IP6 const &b)
{
return a.uint64[0] == b.uint64[0] && a.uint64[1] == b.uint64[1];
}

bool operator==(IP const &a, IP const &b)
{
if (a.family != b.family) {
return false;
}

if (net_family_is_ipv4(a.family)) {
return a.ip.v4 == b.ip.v4;
} else {
return a.ip.v6 == b.ip.v6;
}
}

bool operator==(IP_Port const &a, IP_Port const &b) { return a.ip == b.ip && a.port == b.port; }

std::ostream &operator<<(std::ostream &out, IP const &v)
{
Ip_Ntoa ip_str;
Expand Down
7 changes: 7 additions & 0 deletions toxcore/network_test_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public:
IP_Port operator()();
};

bool operator==(Family const &a, Family const &b);

bool operator==(IP4 const &a, IP4 const &b);
bool operator==(IP6 const &a, IP6 const &b);
bool operator==(IP const &a, IP const &b);
bool operator==(IP_Port const &a, IP_Port const &b);

std::ostream &operator<<(std::ostream &out, IP const &v);
std::ostream &operator<<(std::ostream &out, IP_Port const &v);

Expand Down

0 comments on commit 2f31e05

Please sign in to comment.