]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
auth: Wrap SSql pointers in a unique pointer earlier 13965/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 21 Mar 2024 08:22:24 +0000 (09:22 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 21 Mar 2024 08:22:24 +0000 (09:22 +0100)
Using a unique pointer from the beginning makes the ownership of the
pointer more clear, and prevents leaking the database connection and
associated memory if an exception is raised right away.

modules/gmysqlbackend/gmysqlbackend.cc
modules/godbcbackend/godbcbackend.cc
modules/gpgsqlbackend/gpgsqlbackend.cc
modules/gsqlite3backend/gsqlite3backend.cc
pdns/backends/gsql/gsqlbackend.hh

index 869f819db2bb54f10fac6469042919c643eecb22..7aac1044ae8efa63eecc9f5b2dbf85ba06c7ba49 100644 (file)
@@ -52,17 +52,17 @@ gMySQLBackend::gMySQLBackend(const string& mode, const string& suffix) :
 
 void gMySQLBackend::reconnect()
 {
-  setDB(new SMySQL(getArg("dbname"),
-                   getArg("host"),
-                   getArgAsNum("port"),
-                   getArg("socket"),
-                   getArg("user"),
-                   getArg("password"),
-                   getArg("group"),
-                   mustDo("innodb-read-committed"),
-                   getArgAsNum("timeout"),
-                   mustDo("thread-cleanup"),
-                   mustDo("ssl")));
+  setDB(std::unique_ptr<SSql>(new SMySQL(getArg("dbname"),
+                                         getArg("host"),
+                                         getArgAsNum("port"),
+                                         getArg("socket"),
+                                         getArg("user"),
+                                         getArg("password"),
+                                         getArg("group"),
+                                         mustDo("innodb-read-committed"),
+                                         getArgAsNum("timeout"),
+                                         mustDo("thread-cleanup"),
+                                         mustDo("ssl"))));
   allocateStatements();
 }
 
index 83123d8b03781069e94c3251e4217205c8d10b40..39b34510b17f58ccad8b20d55ab8bfaaa82ce257 100644 (file)
@@ -38,7 +38,7 @@ gODBCBackend::gODBCBackend(const std::string& mode, const std::string& suffix) :
   GSQLBackend(mode, suffix)
 {
   try {
-    setDB(new SODBC(getArg("datasource"), getArg("username"), getArg("password")));
+    setDB(std::unique_ptr<SSql>(new SODBC(getArg("datasource"), getArg("username"), getArg("password"))));
   }
   catch (SSqlException& e) {
     g_log << Logger::Error << mode << " Connection failed: " << e.txtReason() << std::endl;
index f69828aeaff1b47aa0ca243734f38fb16a7d8053..481ac8fd21b625baf454519abdec35730779e2cf 100644 (file)
@@ -40,13 +40,13 @@ gPgSQLBackend::gPgSQLBackend(const string& mode, const string& suffix) :
   GSQLBackend(mode, suffix)
 {
   try {
-    setDB(new SPgSQL(getArg("dbname"),
-                     getArg("host"),
-                     getArg("port"),
-                     getArg("user"),
-                     getArg("password"),
-                     getArg("extra-connection-parameters"),
-                     mustDo("prepared-statements")));
+    setDB(std::unique_ptr<SSql>(new SPgSQL(getArg("dbname"),
+                                           getArg("host"),
+                                           getArg("port"),
+                                           getArg("user"),
+                                           getArg("password"),
+                                           getArg("extra-connection-parameters"),
+                                           mustDo("prepared-statements"))));
   }
 
   catch (SSqlException& e) {
index 829213d6c81b1d55739110e2f3fdea84ef93effc..f54491c8d4be9bf99922b165af87b93e8f6afec4 100644 (file)
@@ -43,15 +43,15 @@ gSQLite3Backend::gSQLite3Backend(const std::string& mode, const std::string& suf
   GSQLBackend(mode, suffix)
 {
   try {
-    SSQLite3* ptr = new SSQLite3(getArg("database"), getArg("pragma-journal-mode"));
-    setDB(ptr);
-    allocateStatements();
+    auto ptr = std::unique_ptr<SSql>(new SSQLite3(getArg("database"), getArg("pragma-journal-mode")));
     if (!getArg("pragma-synchronous").empty()) {
       ptr->execute("PRAGMA synchronous=" + getArg("pragma-synchronous"));
     }
     if (mustDo("pragma-foreign-keys")) {
       ptr->execute("PRAGMA foreign_keys = 1");
     }
+    setDB(std::move(ptr));
+    allocateStatements();
   }
   catch (SSqlException& e) {
     g_log << Logger::Error << mode << ": connection failed: " << e.txtReason() << std::endl;
index 8f92d5fb8c46fa77617541a5a9c7041b7570c416..e54e5b2a3bbcc38fcfcc137a03f13029a592bb3e 100644 (file)
@@ -42,10 +42,10 @@ public:
     d_db.reset();
   }
 
-  void setDB(SSql *db)
+  void setDB(std::unique_ptr<SSql>&& database)
   {
     freeStatements();
-    d_db=std::unique_ptr<SSql>(db);
+    d_db = std::move(database);
     if (d_db) {
       d_db->setLog(::arg().mustDo("query-logging"));
     }