From: Remi Gacogne Date: Wed, 12 Jul 2023 08:37:29 +0000 (+0200) Subject: calidns: Prevent a crash on an empty domains file X-Git-Tag: rec-5.0.0-alpha1~82^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2c72227a2c0e226e925af568d985f2240fed497c;p=thirdparty%2Fpdns.git calidns: Prevent a crash on an empty domains file calidns could have crashed with a null pointer derefence if the main thread exits before the receiver thread has finished, because the vector of sockets no longer existed. --- diff --git a/pdns/calidns.cc b/pdns/calidns.cc index 87bfbe57ed..6bcbcb3a7a 100644 --- a/pdns/calidns.cc +++ b/pdns/calidns.cc @@ -55,10 +55,13 @@ static po::variables_map g_vm; static bool g_quiet; -static void* recvThread(const vector>* sockets) +static void recvThread(const std::shared_ptr>> sockets) { vector rfds, fds; - for(const auto& s : *sockets) { + for (const auto& s : *sockets) { + if (s == nullptr) { + continue; + } struct pollfd pfd; pfd.fd = s->getHandle(); pfd.events = POLLIN; @@ -114,7 +117,6 @@ static void* recvThread(const vector>* sockets) } } } - return 0; } static ComboAddress getRandomAddressFromRange(const Netmask& ecsRange) @@ -383,7 +385,7 @@ try cout<<"Generated "<> sockets; + auto sockets = std::make_shared>>(); ComboAddress dest; try { dest = ComboAddress(g_vm["destination"].as(), 53); @@ -412,9 +414,14 @@ try } } - sockets.push_back(std::move(sock)); + sockets->push_back(std::move(sock)); } - new thread(recvThread, &sockets); + + { + std::thread receiver(recvThread, sockets); + receiver.detach(); + } + uint32_t qps; ofstream plot; @@ -465,7 +472,7 @@ try DTime dt; dt.set(); - sendPackets(sockets, toSend, qps, dest, ecsRange); + sendPackets(*sockets, toSend, qps, dest, ecsRange); const auto udiff = dt.udiffNoReset(); const auto realqps=toSend.size()/(udiff/1000000.0);