fuzz: Implement fuzzed_dns_lookup_function as lambda

This commit is contained in:
practicalswift 2021-03-15 15:20:03 +00:00
parent eceb3f7707
commit 7c8c140ecc

View file

@ -12,27 +12,22 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace {
FuzzedDataProvider* fuzzed_data_provider_ptr = nullptr;
std::vector<CNetAddr> fuzzed_dns_lookup_function(const std::string& name, bool allow_lookup)
{
std::vector<CNetAddr> resolved_addresses;
while (fuzzed_data_provider_ptr->ConsumeBool()) {
resolved_addresses.push_back(ConsumeNetAddr(*fuzzed_data_provider_ptr));
}
return resolved_addresses;
}
} // namespace
FUZZ_TARGET(netbase_dns_lookup) FUZZ_TARGET(netbase_dns_lookup)
{ {
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
fuzzed_data_provider_ptr = &fuzzed_data_provider;
const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512); const std::string name = fuzzed_data_provider.ConsumeRandomLengthString(512);
const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>(); const unsigned int max_results = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
const bool allow_lookup = fuzzed_data_provider.ConsumeBool(); const bool allow_lookup = fuzzed_data_provider.ConsumeBool();
const int default_port = fuzzed_data_provider.ConsumeIntegral<int>(); const int default_port = fuzzed_data_provider.ConsumeIntegral<int>();
auto fuzzed_dns_lookup_function = [&](const std::string&, bool) {
std::vector<CNetAddr> resolved_addresses;
while (fuzzed_data_provider.ConsumeBool()) {
resolved_addresses.push_back(ConsumeNetAddr(fuzzed_data_provider));
}
return resolved_addresses;
};
{ {
std::vector<CNetAddr> resolved_addresses; std::vector<CNetAddr> resolved_addresses;
if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) { if (LookupHost(name, resolved_addresses, max_results, allow_lookup, fuzzed_dns_lookup_function)) {
@ -73,5 +68,4 @@ FUZZ_TARGET(netbase_dns_lookup)
assert(resolved_subnet.IsValid()); assert(resolved_subnet.IsValid());
} }
} }
fuzzed_data_provider_ptr = nullptr;
} }