2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-03 15:35:17 +00:00

[2821] Small memory leak issue in MySQLLeaseMgr constructor

the mysql lease manager wouldn't close the mysql database immediately if initialization failed at certain points.

This proposed fix is not entirely complete; it would probably be better to either use such a holder class as the member, or to slightly clean it up so that the return value from release() can be given to mysql_ (and therefore use the holder in the construction code instead of mysql_), but I did not want to touch too much existing code at this point.
This commit is contained in:
Jelte Jansen
2013-02-25 17:07:58 +01:00
parent 2642fcae53
commit 21dae0aa02

View File

@@ -193,6 +193,32 @@ TaggedStatement tagged_statements[] = {
{MySqlLeaseMgr::NUM_STATEMENTS, NULL} {MySqlLeaseMgr::NUM_STATEMENTS, NULL}
}; };
// Small RAII object for safer initialization, will close the database
// connection upon destruction, unless release() has been called.
class MySQLHolder {
public:
MySQLHolder(MYSQL* mysql) : mysql_(mysql) {}
~MySQLHolder() {
if (mysql_) {
mysql_close(mysql_);
}
}
MYSQL* get_ptr() {
return (mysql_);
}
MYSQL* release() {
MYSQL* ptr = mysql_;
mysql_ = NULL;
return (ptr);
}
private:
MYSQL* mysql_;
};
}; // Anonymous namespace }; // Anonymous namespace
@@ -870,14 +896,15 @@ private:
MYSQL_STMT* statement_; ///< Statement for which results are freed MYSQL_STMT* statement_; ///< Statement for which results are freed
}; };
// MySqlLeaseMgr Constructor and Destructor // MySqlLeaseMgr Constructor and Destructor
MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters) MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
: LeaseMgr(parameters), mysql_(NULL) { : LeaseMgr(parameters), mysql_(NULL) {
// Allocate context for MySQL - it is destroyed in the destructor. // Allocate context for MySQL
mysql_ = mysql_init(NULL); MySQLHolder mysql_holder(mysql_init(NULL));
mysql_ = mysql_holder.get_ptr();
if (mysql_ == NULL) { if (mysql_ == NULL) {
isc_throw(DbOpenError, "unable to initialize MySQL"); isc_throw(DbOpenError, "unable to initialize MySQL");
} }
@@ -902,6 +929,9 @@ MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
// program and the database. // program and the database.
exchange4_.reset(new MySqlLease4Exchange()); exchange4_.reset(new MySqlLease4Exchange());
exchange6_.reset(new MySqlLease6Exchange()); exchange6_.reset(new MySqlLease6Exchange());
// Let the real destructor take care of cleaning up now
mysql_holder.release();
} }