From 9f607f215722a9b1e80f78200f8f47d9c134fc9a Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 31 Dec 2018 10:29:12 +0100 Subject: [PATCH] rec: Define NSECBitmap::nbTypes instead of using 65536 directly --- pdns/dnsrecords.hh | 7 +++++-- pdns/nsecrecords.cc | 4 ++-- pdns/test-dnsrecords_cc.cc | 17 +++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pdns/dnsrecords.hh b/pdns/dnsrecords.hh index 67d77f34f..a8d5bb421 100644 --- a/pdns/dnsrecords.hh +++ b/pdns/dnsrecords.hh @@ -580,10 +580,13 @@ public: void toPacket(DNSPacketWriter& pw); std::string getZoneRepresentation() const; + static constexpr size_t const nbTypes = 65536; + private: + void migrateToBitSet() { - d_bitset = std::unique_ptr>(new std::bitset<65536>()); + d_bitset = std::unique_ptr>(new std::bitset()); for (const auto& type : d_set) { d_bitset->set(type); } @@ -594,7 +597,7 @@ private: when there are a lot of them. So we start with the set, but allocate and switch to a bitset if the number of covered types increases a lot */ - std::unique_ptr> d_bitset; + std::unique_ptr> d_bitset; std::set d_set; }; diff --git a/pdns/nsecrecords.cc b/pdns/nsecrecords.cc index 8149a9346..25132682e 100644 --- a/pdns/nsecrecords.cc +++ b/pdns/nsecrecords.cc @@ -76,7 +76,7 @@ void NSECBitmap::toPacket(DNSPacketWriter& pw) if (d_bitset) { size_t count = d_bitset->count(); size_t found = 0; - for(size_t idx = 0; idx < 65535 && found < count; ++idx){ + for(size_t idx = 0; idx < nbTypes && found < count; ++idx){ if (!d_bitset->test(idx)) { continue; } @@ -138,7 +138,7 @@ string NSECBitmap::getZoneRepresentation() const if (d_bitset) { size_t count = d_bitset->count(); size_t found = 0; - for(size_t idx = 0; idx < 65535 && found < count; ++idx) { + for(size_t idx = 0; idx < nbTypes && found < count; ++idx) { if (!d_bitset->test(idx)) { continue; } diff --git a/pdns/test-dnsrecords_cc.cc b/pdns/test-dnsrecords_cc.cc index e28104721..764d8fdf3 100644 --- a/pdns/test-dnsrecords_cc.cc +++ b/pdns/test-dnsrecords_cc.cc @@ -493,30 +493,39 @@ BOOST_AUTO_TEST_CASE(test_nsec_records_types) { BOOST_AUTO_TEST_CASE(test_nsec3_records_types) { { - auto validNSEC3 = DNSRecordContent::mastermake(QType::NSEC3, QClass::IN, "1 1 12 aabbccdd 2vptu5timamqttgl4luu9kg21e0aor3s A MX RRSIG NSEC3 TYPE1234"); + const std::string str = "1 1 12 aabbccdd 2vptu5timamqttgl4luu9kg21e0aor3s a mx rrsig nsec3 type1234 type65535"; + auto validNSEC3 = DNSRecordContent::mastermake(QType::NSEC3, QClass::IN, str); auto nsec3Content = std::dynamic_pointer_cast(validNSEC3); BOOST_REQUIRE(nsec3Content); - for (const auto type : { QType::A, QType::MX, QType::RRSIG, QType::NSEC3, static_cast(1234) }) { + for (const auto type : { QType::A, QType::MX, QType::RRSIG, QType::NSEC3, static_cast(1234), static_cast(65535) }) { BOOST_CHECK(nsec3Content->isSet(type)); } BOOST_CHECK_EQUAL(nsec3Content->isSet(QType::NSEC), false); - BOOST_CHECK_EQUAL(nsec3Content->numberOfTypesSet(), 5); + BOOST_CHECK_EQUAL(nsec3Content->numberOfTypesSet(), 6); + auto str2 = nsec3Content->getZoneRepresentation(); + boost::to_lower(str2); + BOOST_CHECK_EQUAL(str2, str); } { - auto nsec3Content = std::make_shared(); + std::string str = "1 1 12 aabbccdd 2vptu5timamqttgl4luu9kg21e0aor3s"; + auto nsec3Content = std::make_shared(str); BOOST_CHECK_EQUAL(nsec3Content->numberOfTypesSet(), 0); BOOST_CHECK_EQUAL(nsec3Content->isSet(QType::NSEC), false); for (size_t idx = 0; idx < 65536; idx++) { nsec3Content->set(idx); + str += " " + toLower(DNSRecordContent::NumberToType(idx)); } BOOST_CHECK_EQUAL(nsec3Content->isSet(QType::NSEC), true); BOOST_CHECK_EQUAL(nsec3Content->numberOfTypesSet(), 65536); for (size_t idx = 0; idx < 65536; idx++) { BOOST_CHECK(nsec3Content->isSet(idx)); } + auto str2 = nsec3Content->getZoneRepresentation(); + boost::to_lower(str2); + BOOST_CHECK_EQUAL(str2, str); } { -- 2.40.0