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 af4cb31
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion other/docker/windows/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM debian:bullseye-slim

# Build-time environment variables
ARG VERSION_MSGPACK=4.0.0 \
VERSION_SODIUM=1.0.18 \
VERSION_SODIUM=1.0.19 \
VERSION_OPUS=1.3.1 \
VERSION_VPX=1.11.0 \
\
Expand Down
4 changes: 2 additions & 2 deletions other/docker/windows/build_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ build() {

echo
echo "=== Building Sodium $VERSION_SODIUM $ARCH ==="
curl "${CURL_OPTIONS[@]}" -O "https://download.libsodium.org/libsodium/releases/libsodium-$VERSION_SODIUM.tar.gz"
curl "${CURL_OPTIONS[@]}" -O "https://github.com/jedisct1/libsodium/releases/download/$VERSION_SODIUM-RELEASE/libsodium-$VERSION_SODIUM.tar.gz"
tar -xf "libsodium-$VERSION_SODIUM.tar.gz"
cd "libsodium-$VERSION_SODIUM"
cd "libsodium-stable"
./configure --host="$WINDOWS_TOOLCHAIN" --prefix="$PREFIX_DIR" --disable-shared --enable-static
make
make install
Expand Down
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;

Check warning on line 42 in toxcore/network_test_util.cc

View check run for this annotation

Codecov / codecov/patch

toxcore/network_test_util.cc#L42

Added line #L42 was not covered by tests
}

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 af4cb31

Please sign in to comment.