From: Remi Gacogne Date: Mon, 9 Jan 2023 10:31:12 +0000 (+0100) Subject: dnsdist: Move the exception handling out of handleOnePacket() X-Git-Tag: dnsdist-1.8.0-rc1~129^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e26b058850e755d16a04d29d326ee9102b45f326;p=thirdparty%2Fpdns.git dnsdist: Move the exception handling out of handleOnePacket() --- diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index 19005d2cfa..096e59cc09 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -1774,29 +1774,21 @@ static void udpClientThread(std::vector states) ComboAddress dest; auto handleOnePacket = [&packet, &iov, &holders, &msgh, &remote, &dest, initialBufferSize](const UDPStateParam& param) { - try { - packet.resize(initialBufferSize); - iov.iov_base = &packet.at(0); - iov.iov_len = packet.size(); + packet.resize(initialBufferSize); + iov.iov_base = &packet.at(0); + iov.iov_len = packet.size(); - ssize_t got = recvmsg(param.socket, &msgh, 0); + ssize_t got = recvmsg(param.socket, &msgh, 0); - if (got < 0 || static_cast(got) < sizeof(struct dnsheader)) { - ++g_stats.nonCompliantQueries; - ++param.cs->nonCompliantQueries; - return; - } + if (got < 0 || static_cast(got) < sizeof(struct dnsheader)) { + ++g_stats.nonCompliantQueries; + ++param.cs->nonCompliantQueries; + return; + } - packet.resize(static_cast(got)); + packet.resize(static_cast(got)); - processUDPQuery(*param.cs, holders, &msgh, remote, dest, packet, nullptr, nullptr, nullptr, nullptr); - } - catch (const std::bad_alloc& e) { - /* most exceptions are handled by processUDPQuery(), but we might be out of memory (std::bad_alloc) - in which case we DO NOT want to log (as it would trigger another memory allocation attempt - that might throw as well) but wait a bit (one millisecond) and then try to recover */ - usleep(1000); - } + processUDPQuery(*param.cs, holders, &msgh, remote, dest, packet, nullptr, nullptr, nullptr, nullptr); }; std::vector params; @@ -1812,7 +1804,15 @@ static void udpClientThread(std::vector states) cmsgbuf_aligned cbuf; fillMSGHdr(&msgh, &iov, &cbuf, sizeof(cbuf), reinterpret_cast(&packet.at(0)), param.maxIncomingPacketSize, &remote); while (true) { - handleOnePacket(param); + try { + handleOnePacket(param); + } + catch (const std::bad_alloc& e) { + /* most exceptions are handled by handleOnePacket(), but we might be out of memory (std::bad_alloc) + in which case we DO NOT want to log (as it would trigger another memory allocation attempt + that might throw as well) but wait a bit (one millisecond) and then try to recover */ + usleep(1000); + } } } else {