From: Remi Gacogne Date: Tue, 17 Oct 2023 07:32:42 +0000 (+0200) Subject: dnsdist: More delinting X-Git-Tag: dnsdist-1.9.0-alpha2^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0e781539e2ac6f407f1d3710b3c450a7c8b5e87f;p=thirdparty%2Fpdns.git dnsdist: More delinting --- diff --git a/pdns/dnsdist-lua-actions.cc b/pdns/dnsdist-lua-actions.cc index b9d1b5b3af..5bd1cddfc3 100644 --- a/pdns/dnsdist-lua-actions.cc +++ b/pdns/dnsdist-lua-actions.cc @@ -243,7 +243,7 @@ std::map TeeAction::getStats() const void TeeAction::worker() { setThreadName("dnsdist/TeeWork"); - std::array packet; + std::array packet{}; ssize_t res = 0; const dnsheader_aligned dh(packet.data()); for (;;) { @@ -267,21 +267,27 @@ void TeeAction::worker() d_responses++; } + // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well if (dh->rcode == RCode::NoError) { d_noerrors++; } + // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well else if (dh->rcode == RCode::ServFail) { d_servfails++; } + // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well else if (dh->rcode == RCode::NXDomain) { d_nxdomains++; } + // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well else if (dh->rcode == RCode::Refused) { d_refuseds++; } + // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well else if (dh->rcode == RCode::FormErr) { d_formerrs++; } + // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well else if (dh->rcode == RCode::NotImp) { d_notimps++; } diff --git a/pdns/dnsdist-lua-bindings-dnsquestion.cc b/pdns/dnsdist-lua-bindings-dnsquestion.cc index bf456075c7..6c7cb87f51 100644 --- a/pdns/dnsdist-lua-bindings-dnsquestion.cc +++ b/pdns/dnsdist-lua-bindings-dnsquestion.cc @@ -27,6 +27,7 @@ #include "dnsdist-lua.hh" #include "dnsparser.hh" +// NOLINTNEXTLINE(readability-function-cognitive-complexity): this function declares Lua bindings, even with a good refactoring it will likely blow up the threshold void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) { #ifndef DISABLE_NON_FFI_DQ_BINDINGS @@ -44,7 +45,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) }); luaCtx.registerMember("remoteaddr", [](const DNSQuestion& dq) -> const ComboAddress { return dq.ids.origRemote; }, [](DNSQuestion& dq, const ComboAddress newRemote) { (void) newRemote; }); /* DNSDist DNSQuestion */ - luaCtx.registerMember("dh", [](const DNSQuestion& dq) -> dnsheader* { return const_cast(dq).getMutableHeader(); }, [](DNSQuestion& dq, const dnsheader* dh) { + luaCtx.registerMember("dh", [](const DNSQuestion& dq) -> dnsheader* { return dq.getMutableHeader(); }, [](DNSQuestion& dq, const dnsheader* dh) { dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [&dh](dnsheader& header) { header = *dh; return true; @@ -100,7 +101,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) dq.ids.d_protoBufData->d_requestorID = newValue; }); luaCtx.registerFunction("getDO", [](const DNSQuestion& dq) { - return getEDNSZ(dq) & EDNS_HEADER_FLAG_DO; + return getEDNSZ(dq) & EDNS_HEADER_FLAG_DO; }); luaCtx.registerFunction("getContent", [](const DNSQuestion& dq) { return std::string(reinterpret_cast(dq.getData().data()), dq.getData().size()); @@ -111,7 +112,6 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) buffer.clear(); buffer.insert(buffer.begin(), raw.begin(), raw.end()); - reinterpret_cast(buffer.data())->id = oldID; dnsdist::PacketMangling::editDNSHeaderFromPacket(buffer, [oldID](dnsheader& header) { header.id = oldID; return true; @@ -350,12 +350,15 @@ private: luaCtx.registerMember("qclass", [](const DNSResponse& dq) -> uint16_t { return dq.ids.qclass; }, [](DNSResponse& dq, uint16_t newClass) { (void) newClass; }); luaCtx.registerMember("rcode", [](const DNSResponse& dq) -> int { return dq.getHeader()->rcode; }, [](DNSResponse& dq, int newRCode) { dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [newRCode](dnsheader& header) { - header.rcode = newRCode; - return true; + if (newRCode >= 0 && newRCode <= std::numeric_limits::max()) { + header.rcode = static_cast(newRCode); + return true; + } + return false; }); }); luaCtx.registerMember("remoteaddr", [](const DNSResponse& dq) -> const ComboAddress { return dq.ids.origRemote; }, [](DNSResponse& dq, const ComboAddress newRemote) { (void) newRemote; }); - luaCtx.registerMember("dh", [](const DNSResponse& dr) -> dnsheader* { return const_cast(dr).getMutableHeader(); }, [](DNSResponse& dr, const dnsheader* dh) { + luaCtx.registerMember("dh", [](const DNSResponse& dr) -> dnsheader* { return dr.getMutableHeader(); }, [](DNSResponse& dr, const dnsheader* dh) { dnsdist::PacketMangling::editDNSHeaderFromPacket(dr.getMutableData(), [&dh](dnsheader& header) { header = *dh; return true; diff --git a/pdns/dnsdist.cc b/pdns/dnsdist.cc index d0c0dc6292..04e83a9df6 100644 --- a/pdns/dnsdist.cc +++ b/pdns/dnsdist.cc @@ -237,7 +237,8 @@ static std::unique_ptr> g_delay{nullptr}; std::string DNSQuestion::getTrailingData() const { - const char* message = reinterpret_cast(this->getData().data()); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + const auto* message = reinterpret_cast(this->getData().data()); const uint16_t messageLen = getDNSPacketLength(message, this->getData().size()); return std::string(message + messageLen, this->getData().size() - messageLen); } @@ -256,7 +257,7 @@ bool DNSQuestion::setTrailingData(const std::string& tail) return true; } -bool DNSQuestion::editHeader(std::function editFunction) +bool DNSQuestion::editHeader(const std::function& editFunction) { if (data.size() < sizeof(dnsheader)) { throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer"); @@ -986,14 +987,6 @@ bool processRulesResult(const DNSAction::Action& action, DNSQuestion& dq, std::s static bool applyRulesToQuery(LocalHolders& holders, DNSQuestion& dq, const struct timespec& now) { - auto setRCode = [&dq](uint8_t rcode) { - dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [rcode](dnsheader& header) { - header.rcode = rcode; - header.qr = true; - return true; - }); - }; - if (g_rings.shouldRecordQueries()) { g_rings.insertQuery(now, dq.ids.origRemote, dq.ids.qname, dq.ids.qtype, dq.getData().size(), *dq.getHeader(), dq.getProtocol()); } @@ -1016,6 +1009,14 @@ static bool applyRulesToQuery(LocalHolders& holders, DNSQuestion& dq, const stru } #ifndef DISABLE_DYNBLOCKS + auto setRCode = [&dq](uint8_t rcode) { + dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [rcode](dnsheader& header) { + header.rcode = rcode; + header.qr = true; + return true; + }); + }; + /* the Dynamic Block mechanism supports address and port ranges, so we need to pass the full address and port */ if (auto got = holders.dynNMGBlock->lookup(AddressAndPortRange(dq.ids.origRemote, dq.ids.origRemote.isIPv4() ? 32 : 128, 16))) { auto updateBlockStats = [&got]() { diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index e0e8da324e..acbc45102e 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -89,7 +89,7 @@ struct DNSQuestion return data; } - bool editHeader(std::function editFunction); + bool editHeader(const std::function& editFunction); const dnsheader_aligned getHeader() const { @@ -102,7 +102,7 @@ struct DNSQuestion /* this function is not safe against unaligned access, you should use editHeader() instead, but we need it for the Lua bindings */ - dnsheader* getMutableHeader() + dnsheader* getMutableHeader() const { if (data.size() < sizeof(dnsheader)) { throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer");