Skip to content

Commit

Permalink
fuzz: Check for addrv1 compatibility before using addrv1 serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalswift committed Oct 26, 2020
1 parent d67883d commit 903f3d0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/test/fuzz/deserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <key.h>
#include <merkleblock.h>
#include <net.h>
#include <netbase.h>
#include <node/utxo_snapshot.h>
#include <primitives/block.h>
#include <protocol.h>
Expand Down Expand Up @@ -44,9 +45,9 @@ struct invalid_fuzzing_input_exception : public std::exception {
};

template <typename T>
CDataStream Serialize(const T& obj)
CDataStream Serialize(const T& obj, const int version = INIT_PROTO_VERSION)
{
CDataStream ds(SER_NETWORK, INIT_PROTO_VERSION);
CDataStream ds(SER_NETWORK, version);
ds << obj;
return ds;
}
Expand Down Expand Up @@ -79,9 +80,9 @@ void DeserializeFromFuzzingInput(const std::vector<uint8_t>& buffer, T& obj)
}

template <typename T>
void AssertEqualAfterSerializeDeserialize(const T& obj)
void AssertEqualAfterSerializeDeserialize(const T& obj, const int version = INIT_PROTO_VERSION)
{
assert(Deserialize<T>(Serialize(obj)) == obj);
assert(Deserialize<T>(Serialize(obj, version)) == obj);
}

} // namespace
Expand Down Expand Up @@ -183,7 +184,10 @@ void test_one_input(const std::vector<uint8_t>& buffer)
#elif NETADDR_DESERIALIZE
CNetAddr na;
DeserializeFromFuzzingInput(buffer, na);
AssertEqualAfterSerializeDeserialize(na);
if (na.IsAddrV1Compatible()) {
AssertEqualAfterSerializeDeserialize(na);
}
AssertEqualAfterSerializeDeserialize(na, INIT_PROTO_VERSION | ADDRV2_FORMAT);
#elif SERVICE_DESERIALIZE
CService s;
DeserializeFromFuzzingInput(buffer, s);
Expand Down

0 comments on commit 903f3d0

Please sign in to comment.