Skip to content

Commit

Permalink
Fix a memory leak in forum_user_handler
Browse files Browse the repository at this point in the history
Conflicts:
	src/server/forum_user_handler.cpp
  • Loading branch information
AI0867 committed Jul 6, 2014
1 parent 8c45e30 commit ca2e837
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/server/forum_user_handler.cpp
Expand Up @@ -133,7 +133,8 @@ bool fuh::user_exists(const std::string& name) {

// Make a test query for this username
try {
return mysql_fetch_row(db_query("SELECT username FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')"));
mysql_result res = db_query("SELECT username FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')");
return mysql_fetch_row(res.get());
} catch (error e) {
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl;
// If the database is down just let all usernames log in
Expand Down Expand Up @@ -247,7 +248,13 @@ void fuh::set_lastlogin(const std::string& user, const time_t& lastlogin) {
}
}

MYSQL_RES* fuh::db_query(const std::string& sql) {
struct result_deleter {
void operator()(MYSQL_RES *result) {
mysql_free_result(result);
}
};

fuh::mysql_result fuh::db_query(const std::string& sql) {
if(mysql_query(conn, sql.c_str())) {
WRN_UH << "not connected to database, reconnecting..." << std::endl;
//Try to reconnect and execute query again
Expand All @@ -257,11 +264,12 @@ MYSQL_RES* fuh::db_query(const std::string& sql) {
throw error("Error querying database.");
}
}
return mysql_store_result(conn);
return mysql_result(mysql_store_result(conn), result_deleter());
}

std::string fuh::db_query_to_string(const std::string& sql) {
return std::string(mysql_fetch_row(db_query(sql))[0]);
mysql_result res = db_query(sql);
return std::string(mysql_fetch_row(res.get())[0]);
}


Expand Down Expand Up @@ -291,7 +299,8 @@ bool fuh::extra_row_exists(const std::string& name) {

// Make a test query for this username
try {
return mysql_fetch_row(db_query("SELECT username FROM " + db_extra_table_ + " WHERE UPPER(username)=UPPER('" + name + "')"));
mysql_result res = db_query("SELECT username FROM " + db_extra_table_ + " WHERE UPPER(username)=UPPER('" + name + "')");
return mysql_fetch_row(res.get());
} catch (error e) {
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl;
return false;
Expand Down
5 changes: 4 additions & 1 deletion src/server/forum_user_handler.hpp
Expand Up @@ -19,6 +19,7 @@

#include <vector>

#include <boost/shared_ptr.hpp>
#include <mysql/mysql.h>
#include "../md5.hpp"

Expand Down Expand Up @@ -96,8 +97,10 @@ class fuh : public user_handler {

std::string db_name_, db_host_, db_user_, db_password_, db_users_table_, db_extra_table_;

typedef boost::shared_ptr<MYSQL_RES> mysql_result;

// Throws user_handler::error
MYSQL_RES* db_query(const std::string& query);
mysql_result db_query(const std::string& query);

// Throws user_handler::error via db_query()
std::string db_query_to_string(const std::string& query);
Expand Down

0 comments on commit ca2e837

Please sign in to comment.