From 5144dfa8f9e10797cbe453e7b2b957da9e0050f5 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 2 Apr 2024 16:05:51 +0200 Subject: [PATCH] auth: Wrap backend factories in smart pointers --- docs/appendices/backend-writers-guide.rst | 2 +- modules/bindbackend/bindbackend2.cc | 2 +- modules/geoipbackend/geoipbackend.cc | 2 +- modules/gmysqlbackend/gmysqlbackend.cc | 2 +- modules/godbcbackend/godbcbackend.cc | 2 +- modules/gpgsqlbackend/gpgsqlbackend.cc | 2 +- modules/gsqlite3backend/gsqlite3backend.cc | 2 +- modules/ldapbackend/ldapbackend.cc | 4 +--- modules/lmdbbackend/lmdbbackend.cc | 2 +- modules/lua2backend/lua2backend.cc | 2 +- modules/pipebackend/pipebackend.cc | 2 +- modules/remotebackend/remotebackend.cc | 2 +- modules/tinydnsbackend/tinydnsbackend.cc | 2 +- pdns/dnsbackend.cc | 10 +++------- pdns/dnsbackend.hh | 4 ++-- pdns/test-ueberbackend_cc.cc | 16 ++++++++-------- 16 files changed, 26 insertions(+), 32 deletions(-) diff --git a/docs/appendices/backend-writers-guide.rst b/docs/appendices/backend-writers-guide.rst index 1ee3046a95..6802169fad 100644 --- a/docs/appendices/backend-writers-guide.rst +++ b/docs/appendices/backend-writers-guide.rst @@ -217,7 +217,7 @@ furthermore, only about its A record: public: RandomLoader() { - BackendMakers().report(new RandomFactory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[randombackend] This is the random backend version " VERSION " reporting" << endl; } }; diff --git a/modules/bindbackend/bindbackend2.cc b/modules/bindbackend/bindbackend2.cc index 013c9eb8ac..3c9b5409d7 100644 --- a/modules/bindbackend/bindbackend2.cc +++ b/modules/bindbackend/bindbackend2.cc @@ -1535,7 +1535,7 @@ class Bind2Loader public: Bind2Loader() { - BackendMakers().report(new Bind2Factory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[bind2backend] This is the bind backend version " << VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/geoipbackend/geoipbackend.cc b/modules/geoipbackend/geoipbackend.cc index 014aa24f00..5d06d2f6cc 100644 --- a/modules/geoipbackend/geoipbackend.cc +++ b/modules/geoipbackend/geoipbackend.cc @@ -1199,7 +1199,7 @@ class GeoIPLoader public: GeoIPLoader() { - BackendMakers().report(new GeoIPFactory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[geoipbackend] This is the geoip backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/gmysqlbackend/gmysqlbackend.cc b/modules/gmysqlbackend/gmysqlbackend.cc index 82f0f9d285..6ab5d9086c 100644 --- a/modules/gmysqlbackend/gmysqlbackend.cc +++ b/modules/gmysqlbackend/gmysqlbackend.cc @@ -184,7 +184,7 @@ public: //! This reports us to the main UeberBackend class gMySQLLoader() { - BackendMakers().report(new gMySQLFactory("gmysql")); + BackendMakers().report(std::make_unique("gmysql")); g_log << Logger::Info << "[gmysqlbackend] This is the gmysql backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/godbcbackend/godbcbackend.cc b/modules/godbcbackend/godbcbackend.cc index 39b34510b1..d781a51404 100644 --- a/modules/godbcbackend/godbcbackend.cc +++ b/modules/godbcbackend/godbcbackend.cc @@ -169,7 +169,7 @@ public: //! This reports us to the main UeberBackend class gODBCLoader() { - BackendMakers().report(new gODBCFactory("godbc")); + BackendMakers().report(std::make_unique("godbc")); g_log << Logger::Warning << "This is module godbcbackend reporting" << std::endl; } }; diff --git a/modules/gpgsqlbackend/gpgsqlbackend.cc b/modules/gpgsqlbackend/gpgsqlbackend.cc index 481ac8fd21..ea51b68c63 100644 --- a/modules/gpgsqlbackend/gpgsqlbackend.cc +++ b/modules/gpgsqlbackend/gpgsqlbackend.cc @@ -192,7 +192,7 @@ public: //! This reports us to the main UeberBackend class gPgSQLLoader() { - BackendMakers().report(new gPgSQLFactory("gpgsql")); + BackendMakers().report(std::make_unique("gpgsql")); g_log << Logger::Info << "[gpgsqlbackend] This is the gpgsql backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/gsqlite3backend/gsqlite3backend.cc b/modules/gsqlite3backend/gsqlite3backend.cc index f54491c8d4..09ef25fb6b 100644 --- a/modules/gsqlite3backend/gsqlite3backend.cc +++ b/modules/gsqlite3backend/gsqlite3backend.cc @@ -179,7 +179,7 @@ public: //! This reports us to the main UeberBackend class gSQLite3Loader() { - BackendMakers().report(new gSQLite3Factory("gsqlite3")); + BackendMakers().report(std::make_unique("gsqlite3")); g_log << Logger::Info << "[gsqlite3] This is the gsqlite3 backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/ldapbackend/ldapbackend.cc b/modules/ldapbackend/ldapbackend.cc index f400559e70..c1e91b692f 100644 --- a/modules/ldapbackend/ldapbackend.cc +++ b/modules/ldapbackend/ldapbackend.cc @@ -293,12 +293,10 @@ public: class LdapLoader { - LdapFactory factory; - public: LdapLoader() { - BackendMakers().report(&factory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[ldapbackend] This is the ldap backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/lmdbbackend/lmdbbackend.cc b/modules/lmdbbackend/lmdbbackend.cc index 6038d989e0..2997e99112 100644 --- a/modules/lmdbbackend/lmdbbackend.cc +++ b/modules/lmdbbackend/lmdbbackend.cc @@ -2792,7 +2792,7 @@ class LMDBLoader public: LMDBLoader() { - BackendMakers().report(new LMDBFactory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[lmdbbackend] This is the lmdb backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/lua2backend/lua2backend.cc b/modules/lua2backend/lua2backend.cc index 9a596e0ac1..a00f6c25eb 100644 --- a/modules/lua2backend/lua2backend.cc +++ b/modules/lua2backend/lua2backend.cc @@ -62,7 +62,7 @@ class Lua2Loader public: Lua2Loader() { - BackendMakers().report(new Lua2Factory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[lua2backend] This is the lua2 backend version " VERSION #ifndef REPRODUCIBLE diff --git a/modules/pipebackend/pipebackend.cc b/modules/pipebackend/pipebackend.cc index e79075a7f3..68173dc3a4 100644 --- a/modules/pipebackend/pipebackend.cc +++ b/modules/pipebackend/pipebackend.cc @@ -379,7 +379,7 @@ class PipeLoader public: PipeLoader() { - BackendMakers().report(new PipeFactory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << kBackendId << " This is the pipe backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/remotebackend/remotebackend.cc b/modules/remotebackend/remotebackend.cc index 887d2b8f4a..d4d68c97bf 100644 --- a/modules/remotebackend/remotebackend.cc +++ b/modules/remotebackend/remotebackend.cc @@ -1007,7 +1007,7 @@ public: RemoteLoader::RemoteLoader() { - BackendMakers().report(new RemoteBackendFactory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << kBackendId << " This is the remote backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/modules/tinydnsbackend/tinydnsbackend.cc b/modules/tinydnsbackend/tinydnsbackend.cc index 6ba897fb48..15c86b3c68 100644 --- a/modules/tinydnsbackend/tinydnsbackend.cc +++ b/modules/tinydnsbackend/tinydnsbackend.cc @@ -393,7 +393,7 @@ class TinyDNSLoader public: TinyDNSLoader() { - BackendMakers().report(new TinyDNSFactory); + BackendMakers().report(std::make_unique()); g_log << Logger::Info << "[tinydnsbackend] This is the tinydns backend version " VERSION #ifndef REPRODUCIBLE << " (" __DATE__ " " __TIME__ ")" diff --git a/pdns/dnsbackend.cc b/pdns/dnsbackend.cc index 8f45251a61..1aca03ce48 100644 --- a/pdns/dnsbackend.cc +++ b/pdns/dnsbackend.cc @@ -85,18 +85,14 @@ BackendMakerClass& BackendMakers() return bmc; } -void BackendMakerClass::report(BackendFactory* backendFactory) +void BackendMakerClass::report(std::unique_ptr&& backendFactory) { - d_repository[backendFactory->getName()] = backendFactory; + d_repository[backendFactory->getName()] = std::move(backendFactory); } void BackendMakerClass::clear() { d_instances.clear(); - for (auto& repo : d_repository) { - delete repo.second; - repo.second = nullptr; - } d_repository.clear(); } @@ -199,7 +195,7 @@ vector> BackendMakerClass::all(bool metadataOnly) try { for (const auto& instance : d_instances) { current = instance.first + instance.second; - auto* repo = d_repository[instance.first]; + const auto& repo = d_repository[instance.first]; std::unique_ptr made{metadataOnly ? repo->makeMetadataOnly(instance.second) : repo->make(instance.second)}; if (made == nullptr) { throw PDNSException("Unable to launch backend '" + instance.first + "'"); diff --git a/pdns/dnsbackend.hh b/pdns/dnsbackend.hh index d79f851f73..7b7e4c2117 100644 --- a/pdns/dnsbackend.hh +++ b/pdns/dnsbackend.hh @@ -500,7 +500,7 @@ private: class BackendMakerClass { public: - void report(BackendFactory* backendFactory); + void report(std::unique_ptr&& backendFactory); void launch(const string& instr); vector> all(bool metadataOnly = false); static void load(const string& module); @@ -510,7 +510,7 @@ public: private: static void load_all(); - using d_repository_t = map; + using d_repository_t = map>; d_repository_t d_repository; vector> d_instances; }; diff --git a/pdns/test-ueberbackend_cc.cc b/pdns/test-ueberbackend_cc.cc index b6a1cc3c12..5fc7d0d48a 100644 --- a/pdns/test-ueberbackend_cc.cc +++ b/pdns/test-ueberbackend_cc.cc @@ -467,7 +467,7 @@ BOOST_AUTO_TEST_CASE(test_simple) { zoneA.d_records->insert(SimpleBackend::SimpleDNSRecord(DNSName("geo.powerdns.com."), QType::A, "192.168.0.42", 60)); SimpleBackend::s_zones[1].insert(zoneA); - BackendMakers().report(new SimpleBackendFactory()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackend:1"); UeberBackend::go(); @@ -580,7 +580,7 @@ BOOST_AUTO_TEST_CASE(test_multi_backends_separate_zones) { zoneB.d_records->insert(SimpleBackend::SimpleDNSRecord(DNSName("geo.powerdns.org."), QType::AAAA, "2001:db8::42", 60)); SimpleBackend::s_zones[2].insert(zoneB); - BackendMakers().report(new SimpleBackendFactory()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackend:1, SimpleBackend:2"); UeberBackend::go(); @@ -725,7 +725,7 @@ BOOST_AUTO_TEST_CASE(test_multi_backends_overlay) { zoneB.d_records->insert(SimpleBackend::SimpleDNSRecord(DNSName("geo.powerdns.com."), QType::A, "192.168.0.42", 60)); SimpleBackend::s_zones[2].insert(zoneB); - BackendMakers().report(new SimpleBackendFactory()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackend:1, SimpleBackend:2"); UeberBackend::go(); @@ -852,7 +852,7 @@ BOOST_AUTO_TEST_CASE(test_multi_backends_overlay_name) { zoneB.d_records->insert(SimpleBackend::SimpleDNSRecord(DNSName("geo.powerdns.com."), QType::A, "192.168.0.42", 60)); SimpleBackend::s_zones[2].insert(zoneB); - BackendMakers().report(new SimpleBackendFactory()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackend:1, SimpleBackend:2"); UeberBackend::go(); @@ -976,7 +976,7 @@ BOOST_AUTO_TEST_CASE(test_child_zone) { zoneB.d_records->insert(SimpleBackend::SimpleDNSRecord(DNSName("ns1.powerdns.com."), QType::A, "192.0.2.1", 3600)); SimpleBackend::s_zones[2].insert(zoneB); - BackendMakers().report(new SimpleBackendFactory()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackend:1, SimpleBackend:2"); UeberBackend::go(); @@ -1049,8 +1049,8 @@ BOOST_AUTO_TEST_CASE(test_multi_backends_best_soa) { zoneB.d_records->insert(SimpleBackend::SimpleDNSRecord(DNSName("0.1.0.0.2.ip6.arpa."), QType::SOA, "ns.apnic.net. read-txt-record-of-zone-first-dns-admin.apnic.net. 3005126844 7200 1800 604800 3600", 3600)); SimpleBackend::s_zones[2].insert(zoneB); - BackendMakers().report(new SimpleBackendFactory()); - BackendMakers().report(new SimpleBackendBestAuthFactory()); + BackendMakers().report(std::make_unique()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackendBestAuth:1, SimpleBackend:2"); UeberBackend::go(); @@ -1112,7 +1112,7 @@ BOOST_AUTO_TEST_CASE(test_multi_backends_metadata) { SimpleBackend::s_zones[2].insert(zoneB); SimpleBackend::s_metadata[2].insert(SimpleBackend::SimpleMetaData(DNSName("powerdns.org."), "test-data-b", { "value1", "value2"})); - BackendMakers().report(new SimpleBackendFactory()); + BackendMakers().report(std::make_unique()); BackendMakers().launch("SimpleBackend:1, SimpleBackend:2"); UeberBackend::go(); -- 2.47.2