mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-09-03 23:45:27 +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:
@@ -193,6 +193,32 @@ TaggedStatement tagged_statements[] = {
|
||||
{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
|
||||
|
||||
|
||||
@@ -870,14 +896,15 @@ private:
|
||||
MYSQL_STMT* statement_; ///< Statement for which results are freed
|
||||
};
|
||||
|
||||
|
||||
// MySqlLeaseMgr Constructor and Destructor
|
||||
|
||||
MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
|
||||
: LeaseMgr(parameters), mysql_(NULL) {
|
||||
|
||||
// Allocate context for MySQL - it is destroyed in the destructor.
|
||||
mysql_ = mysql_init(NULL);
|
||||
// Allocate context for MySQL
|
||||
MySQLHolder mysql_holder(mysql_init(NULL));
|
||||
|
||||
mysql_ = mysql_holder.get_ptr();
|
||||
if (mysql_ == NULL) {
|
||||
isc_throw(DbOpenError, "unable to initialize MySQL");
|
||||
}
|
||||
@@ -902,6 +929,9 @@ MySqlLeaseMgr::MySqlLeaseMgr(const LeaseMgr::ParameterMap& parameters)
|
||||
// program and the database.
|
||||
exchange4_.reset(new MySqlLease4Exchange());
|
||||
exchange6_.reset(new MySqlLease6Exchange());
|
||||
|
||||
// Let the real destructor take care of cleaning up now
|
||||
mysql_holder.release();
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user