fuzz: Check for addrv1 compatibility before using addrv1 serializer/deserializer on CSubNet

pull/764/head
practicalswift 4 years ago
parent 7e373294a5
commit 0e3a78a8ab

@ -15,6 +15,7 @@
#include <net.h> #include <net.h>
#include <netbase.h> #include <netbase.h>
#include <node/utxo_snapshot.h> #include <node/utxo_snapshot.h>
#include <optional.h>
#include <primitives/block.h> #include <primitives/block.h>
#include <protocol.h> #include <protocol.h>
#include <psbt.h> #include <psbt.h>
@ -61,15 +62,19 @@ T Deserialize(CDataStream ds)
} }
template <typename T> template <typename T>
void DeserializeFromFuzzingInput(const std::vector<uint8_t>& buffer, T& obj) void DeserializeFromFuzzingInput(const std::vector<uint8_t>& buffer, T& obj, const Optional<int> protocol_version = nullopt)
{ {
CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION); CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
try { if (protocol_version) {
int version; ds.SetVersion(*protocol_version);
ds >> version; } else {
ds.SetVersion(version); try {
} catch (const std::ios_base::failure&) { int version;
throw invalid_fuzzing_input_exception(); ds >> version;
ds.SetVersion(version);
} catch (const std::ios_base::failure&) {
throw invalid_fuzzing_input_exception();
}
} }
try { try {
ds >> obj; ds >> obj;
@ -125,9 +130,15 @@ void test_one_input(const std::vector<uint8_t>& buffer)
CScript script; CScript script;
DeserializeFromFuzzingInput(buffer, script); DeserializeFromFuzzingInput(buffer, script);
#elif SUB_NET_DESERIALIZE #elif SUB_NET_DESERIALIZE
CSubNet sub_net; CSubNet sub_net_1;
DeserializeFromFuzzingInput(buffer, sub_net); DeserializeFromFuzzingInput(buffer, sub_net_1, INIT_PROTO_VERSION);
AssertEqualAfterSerializeDeserialize(sub_net); AssertEqualAfterSerializeDeserialize(sub_net_1, INIT_PROTO_VERSION);
CSubNet sub_net_2;
DeserializeFromFuzzingInput(buffer, sub_net_2, INIT_PROTO_VERSION | ADDRV2_FORMAT);
AssertEqualAfterSerializeDeserialize(sub_net_2, INIT_PROTO_VERSION | ADDRV2_FORMAT);
CSubNet sub_net_3;
DeserializeFromFuzzingInput(buffer, sub_net_3);
AssertEqualAfterSerializeDeserialize(sub_net_3, INIT_PROTO_VERSION | ADDRV2_FORMAT);
#elif TX_IN_DESERIALIZE #elif TX_IN_DESERIALIZE
CTxIn tx_in; CTxIn tx_in;
DeserializeFromFuzzingInput(buffer, tx_in); DeserializeFromFuzzingInput(buffer, tx_in);

Loading…
Cancel
Save