From 3e2324c0c2d16f3056749d81884193d9093352df Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Sun, 8 Dec 2024 16:58:46 -0800 Subject: [PATCH 1/3] clang-tidy: use std::min/max Found with readability-use-std-min-max Signed-off-by: Rosen Penev --- pdns/dnspacket.cc | 4 ++-- pdns/dnsparser.cc | 4 +--- pdns/lua-record.cc | 18 +++++++----------- pdns/sillyrecords.cc | 4 ++-- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/pdns/dnspacket.cc b/pdns/dnspacket.cc index 8588f2c21a15..2afde53a3596 100644 --- a/pdns/dnspacket.cc +++ b/pdns/dnspacket.cc @@ -23,6 +23,7 @@ #include "config.h" #endif #include "utility.hh" +#include #include #include #include @@ -245,8 +246,7 @@ unsigned int DNSPacket::getMinTTL() { unsigned int minttl = UINT_MAX; for(const DNSZoneRecord& rr : d_rrs) { - if (rr.dr.d_ttl < minttl) - minttl = rr.dr.d_ttl; + minttl = std::min(minttl, rr.dr.d_ttl); } return minttl; diff --git a/pdns/dnsparser.cc b/pdns/dnsparser.cc index 12974ae57cea..8a248587d78e 100644 --- a/pdns/dnsparser.cc +++ b/pdns/dnsparser.cc @@ -1016,9 +1016,7 @@ uint32_t getDNSPacketMinTTL(const char* packet, size_t length, bool* seenAuthSOA } const uint32_t ttl = dpm.get32BitInt(); - if (result > ttl) { - result = ttl; - } + result = std::min(result, ttl); dpm.skipRData(); } diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 1f2344a881b5..b06c163061cd 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -1096,18 +1096,14 @@ static void setupLuaRecords(LuaContext& lua) // NOLINT(readability-function-cogn * @example ifportup(443, { '1.2.3.4', '5.4.3.2' })" */ lua.writeFunction("ifportup", [](int port, const boost::variant& ips, const boost::optional> options) { - if (port < 0) { - port = 0; - } - if (port > std::numeric_limits::max()) { - port = std::numeric_limits::max(); - } + port = std::max(port, 0); + port = std::min(port, static_cast(std::numeric_limits::max())); - auto checker = [](const ComboAddress& addr, const opts_t& opts) { - return g_up.isUp(addr, opts); - }; - return genericIfUp(ips, options, checker, port); - }); + auto checker = [](const ComboAddress& addr, const opts_t& opts) { + return g_up.isUp(addr, opts); + }; + return genericIfUp(ips, options, checker, port); + }); lua.writeFunction("ifurlextup", [](const vector >& ipurls, boost::optional options) { vector candidates; diff --git a/pdns/sillyrecords.cc b/pdns/sillyrecords.cc index cc04a3c29f39..a6edc189e290 100644 --- a/pdns/sillyrecords.cc +++ b/pdns/sillyrecords.cc @@ -2,6 +2,7 @@ #include "config.h" #endif +#include #include #include "utility.hh" @@ -46,8 +47,7 @@ static uint8_t precsize_aton(const char **strptr) break; mantissa = cmval / poweroften[exponent]; - if (mantissa > 9) - mantissa = 9; + mantissa = std::min(mantissa, 9); retval = (mantissa << 4) | exponent; From e29f989dd7f153b28d7c2bd8ef7d59e0dc249a0c Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 13 Dec 2024 12:32:37 -0800 Subject: [PATCH 2/3] use min_element Signed-off-by: Rosen Penev --- pdns/dnspacket.cc | 9 ++++----- pdns/dnsparser.hh | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pdns/dnspacket.cc b/pdns/dnspacket.cc index 2afde53a3596..832f89b269e3 100644 --- a/pdns/dnspacket.cc +++ b/pdns/dnspacket.cc @@ -244,12 +244,11 @@ bool DNSPacket::couldBeCached() const unsigned int DNSPacket::getMinTTL() { - unsigned int minttl = UINT_MAX; - for(const DNSZoneRecord& rr : d_rrs) { - minttl = std::min(minttl, rr.dr.d_ttl); + auto it = std::min_element(d_rrs.begin(), d_rrs.end()); + if (it != d_rrs.end()) { + return it->dr.d_ttl; } - - return minttl; + return UINT_MAX; } bool DNSPacket::isEmpty() diff --git a/pdns/dnsparser.hh b/pdns/dnsparser.hh index fd2f5b7112f3..cefeb792206c 100644 --- a/pdns/dnsparser.hh +++ b/pdns/dnsparser.hh @@ -438,6 +438,10 @@ struct DNSZoneRecord bool auth{true}; bool disabled{false}; DNSRecord dr; + + bool operator<(const DNSZoneRecord& other) const { + return dr.d_ttl < other.dr.d_ttl; + } }; class UnknownRecordContent : public DNSRecordContent From 7f9a6a9d51c217e0e92e6d054c39baf376ce2957 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Mon, 20 Jan 2025 12:06:01 +0100 Subject: [PATCH 3/3] clang-tidy --- pdns/dnspacket.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pdns/dnspacket.cc b/pdns/dnspacket.cc index 832f89b269e3..ac6daa92e682 100644 --- a/pdns/dnspacket.cc +++ b/pdns/dnspacket.cc @@ -244,9 +244,9 @@ bool DNSPacket::couldBeCached() const unsigned int DNSPacket::getMinTTL() { - auto it = std::min_element(d_rrs.begin(), d_rrs.end()); - if (it != d_rrs.end()) { - return it->dr.d_ttl; + auto iter = std::min_element(d_rrs.begin(), d_rrs.end()); + if (iter != d_rrs.end()) { + return iter->dr.d_ttl; } return UINT_MAX; }