From: Otto Moerbeek Date: Wed, 26 Jun 2024 12:53:33 +0000 (+0200) Subject: Tidy shuffle.cc X-Git-Tag: rec-5.2.0-alpha1~207^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d321bdbb1bef57ea650b8613fe0610106058b299;p=thirdparty%2Fpdns.git Tidy shuffle.cc --- diff --git a/pdns/shuffle.cc b/pdns/shuffle.cc index 5f96fcd39b..0e507a76d5 100644 --- a/pdns/shuffle.cc +++ b/pdns/shuffle.cc @@ -32,7 +32,8 @@ // shuffle, maintaining some semblance of order void pdns::shuffle(std::vector& rrs) { - std::vector::iterator first, second; + std::vector::iterator first; + std::vector::iterator second; // We assume the CNAMES are listed first in the ANSWER section and the the other records // and we want to shuffle the other records only @@ -44,14 +45,16 @@ void pdns::shuffle(std::vector& rrs) } } // And then for one past the last ANSWER record - for (second = first; second != rrs.end(); ++second) - if (second->dr.d_place != DNSResourceRecord::ANSWER) + for (second = first; second != rrs.end(); ++second) { + if (second->dr.d_place != DNSResourceRecord::ANSWER) { break; + } + } // Now shuffle the non-CNAME ANSWER records - dns_random_engine r; + dns_random_engine randomEngine; if (second - first > 1) { - shuffle(first, second, r); + shuffle(first, second, randomEngine); } // now shuffle the ADDITIONAL records in the same manner as the ANSWER records @@ -67,7 +70,7 @@ void pdns::shuffle(std::vector& rrs) } if (second - first > 1) { - shuffle(first, second, r); + shuffle(first, second, randomEngine); } // we don't shuffle the rest } @@ -76,7 +79,9 @@ void pdns::shuffle(std::vector& rrs) static void shuffle(std::vector& rrs, bool includingAdditionals) { // This shuffles in the same style as the above method, keeping CNAME in the front and RRSIGs at the end - std::vector::iterator first, second; + std::vector::iterator first; + std::vector::iterator second; + for (first = rrs.begin(); first != rrs.end(); ++first) { if (first->d_place == DNSResourceRecord::ANSWER && first->d_type != QType::CNAME) { break; @@ -88,9 +93,9 @@ static void shuffle(std::vector& rrs, bool includingAdditionals) } } - pdns::dns_random_engine r; + pdns::dns_random_engine randomEngine; if (second - first > 1) { - shuffle(first, second, r); + shuffle(first, second, randomEngine); } if (!includingAdditionals) { @@ -110,27 +115,28 @@ static void shuffle(std::vector& rrs, bool includingAdditionals) } if (second - first > 1) { - shuffle(first, second, r); + shuffle(first, second, randomEngine); } // we don't shuffle the rest } static uint16_t mapTypesToOrder(uint16_t type) { - if (type == QType::CNAME) + if (type == QType::CNAME) { return 0; - if (type == QType::RRSIG) + } + if (type == QType::RRSIG) { return 65535; - else - return 1; + } + return 1; } // make sure rrs is sorted in d_place order to avoid surprises later // then shuffle the parts that desire shuffling void pdns::orderAndShuffle(vector& rrs, bool includingAdditionals) { - std::stable_sort(rrs.begin(), rrs.end(), [](const DNSRecord& a, const DNSRecord& b) { - return std::tuple(a.d_place, mapTypesToOrder(a.d_type)) < std::tuple(b.d_place, mapTypesToOrder(b.d_type)); + std::stable_sort(rrs.begin(), rrs.end(), [](const DNSRecord& lhs, const DNSRecord& rhs) { + return std::tuple(lhs.d_place, mapTypesToOrder(lhs.d_type)) < std::tuple(rhs.d_place, mapTypesToOrder(rhs.d_type)); }); shuffle(rrs, includingAdditionals); }