From: Remi Gacogne Date: Wed, 16 Aug 2023 08:15:07 +0000 (+0200) Subject: dnsbulktest: Fix Coverity CID 1401681 and 1401676 X-Git-Tag: rec-5.0.0-alpha1~55^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=014516a0d8e9494d8e19695546e3f3ee8bc258e0;p=thirdparty%2Fpdns.git dnsbulktest: Fix Coverity CID 1401681 and 1401676 --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 625bc1e044..43a464f8ab 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -984,6 +984,7 @@ dnsbulktest_SOURCES = \ dnsparser.cc \ dnsrecords.cc \ dnswriter.cc \ + iputils.cc iputils.hh \ logger.cc \ misc.cc \ nsecrecords.cc \ diff --git a/pdns/dnsbulktest.cc b/pdns/dnsbulktest.cc index 0cba797fa2..b6fbc7bcbf 100644 --- a/pdns/dnsbulktest.cc +++ b/pdns/dnsbulktest.cc @@ -56,7 +56,7 @@ bool g_envoutput=false; struct DNSResult { vector ips; - int rcode; + int rcode{0}; bool seenauthsoa{false}; }; @@ -69,42 +69,40 @@ struct TypedQuery struct SendReceive { - typedef int Identifier; - typedef DNSResult Answer; // ip - int d_socket; + using Identifier = int; + using Answer = DNSResult; // ip + Socket d_socket; std::deque d_idqueue; - typedef accumulator_set< + using acc_t = accumulator_set< double , stats - > acc_t; + >; unique_ptr d_acc; + + static constexpr std::array s_probs{{0.001,0.01, 0.025, 0.1, 0.25,0.5,0.75,0.9,0.975, 0.99,0.9999}}; + unsigned int d_errors{0}; + unsigned int d_nxdomains{0}; + unsigned int d_nodatas{0}; + unsigned int d_oks{0}; + unsigned int d_unknowns{0}; + unsigned int d_received{0}; + unsigned int d_receiveerrors{0}; + unsigned int d_senderrors{0}; - boost::array d_probs; - - SendReceive(const std::string& remoteAddr, uint16_t port) : d_probs({{0.001,0.01, 0.025, 0.1, 0.25,0.5,0.75,0.9,0.975, 0.99,0.9999}}) + SendReceive(const std::string& remoteAddr, uint16_t port) : + d_socket(AF_INET, SOCK_DGRAM), + d_acc(make_unique(acc_t(boost::accumulators::tag::extended_p_square::probabilities=s_probs))) { - d_acc = make_unique(acc_t(boost::accumulators::tag::extended_p_square::probabilities=d_probs)); - // - //d_acc = acc_t - d_socket = socket(AF_INET, SOCK_DGRAM, 0); - int val=1; - setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); - + d_socket.setReuseAddr(); ComboAddress remote(remoteAddr, port); - connect(d_socket, (struct sockaddr*)&remote, remote.getSocklen()); - d_oks = d_errors = d_nodatas = d_nxdomains = d_unknowns = 0; - d_received = d_receiveerrors = d_senderrors = 0; - for(unsigned int id =0 ; id < std::numeric_limits::max(); ++id) + d_socket.connect(remote); + for (unsigned int id =0 ; id < std::numeric_limits::max(); ++id) { d_idqueue.push_back(id); - } - - ~SendReceive() - { - close(d_socket); + } } Identifier send(TypedQuery& domain) @@ -116,7 +114,7 @@ struct SendReceive DNSPacketWriter pw(packet, domain.name, domain.type); - if(d_idqueue.empty()) { + if (d_idqueue.empty()) { cerr<<"Exhausted ids!"<rd = 1; pw.getHeader()->qr = 0; - if(::send(d_socket, &*packet.begin(), packet.size(), 0) < 0) + if (::send(d_socket.getHandle(), &*packet.begin(), packet.size(), 0) < 0) { d_senderrors++; + } - if(!g_quiet) + if (!g_quiet) { cout<<"Sent out query for '"<id<id; } bool receive(Identifier& id, DNSResult& dr) { - if(waitForData(d_socket, 0, 500000) > 0) { - char buf[512]; + if (waitForData(d_socket.getHandle(), 0, 500000) > 0) { + std::array buf; - int len = recv(d_socket, buf, sizeof(buf), 0); - if(len < 0) { + auto len = recv(d_socket.getHandle(), buf.data(), buf.size(), 0); + if (len < 0) { d_receiveerrors++; - return 0; - } - else { - d_received++; + return false; } + d_received++; // parse packet, set 'id', fill out 'ip' - MOADNSParser mdp(false, string(buf, len)); + MOADNSParser mdp(false, string(buf.data(), static_cast(len))); if(!g_quiet) { cout<<"Reply to question for qname='"<