Skip to content

Commit

Permalink
Ensure that mysql_stmt_free_result is called even if exception is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
loonycyborg committed Feb 25, 2017
1 parent 0e45869 commit 8c3c0ab
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/server/mysql_prepared_statement.ipp
Expand Up @@ -21,6 +21,9 @@
#include <string>
#include <string.h>

#define BOOST_SCOPE_EXIT_CONFIG_USE_LAMBDAS
#include <boost/scope_exit.hpp>

#include <mysql/mysql.h>

#include "exceptions.hpp"
Expand Down Expand Up @@ -102,6 +105,10 @@ template<> std::string fetch_result<std::string>(MYSQL_STMT* stmt, const std::st
if(mysql_stmt_bind_result(stmt, result_bind) != 0)
throw sql_error(mysql_stmt_error(stmt), sql);

BOOST_SCOPE_EXIT(&stmt) {
mysql_stmt_free_result(stmt);
} ;

int res = mysql_stmt_fetch(stmt);
if(len > 0) {
buf = new char[len];
Expand All @@ -117,7 +124,6 @@ template<> std::string fetch_result<std::string>(MYSQL_STMT* stmt, const std::st
throw sql_error("null value returned", sql);
if(res != 0)
throw sql_error(mysql_stmt_error(stmt), sql);
mysql_stmt_free_result(stmt);
return result;
}

Expand All @@ -130,14 +136,17 @@ template<> int fetch_result<int>(MYSQL_STMT* stmt, const std::string& sql)
if(mysql_stmt_bind_result(stmt, result_bind) != 0)
throw sql_error(mysql_stmt_error(stmt), sql);

BOOST_SCOPE_EXIT(&stmt) {
mysql_stmt_free_result(stmt);
} ;

int res = mysql_stmt_fetch(stmt);
if(res == MYSQL_NO_DATA)
throw sql_error("no data returned", sql);
if(is_null)
throw sql_error("null value returned", sql);
if(res != 0)
throw sql_error(mysql_stmt_error(stmt), sql);
mysql_stmt_free_result(stmt);
return result;
}

Expand All @@ -150,14 +159,17 @@ template<> bool fetch_result<bool>(MYSQL_STMT* stmt, const std::string& sql)
if(mysql_stmt_bind_result(stmt, result_bind) != 0)
throw sql_error(mysql_stmt_error(stmt), sql);

BOOST_SCOPE_EXIT(&stmt) {
mysql_stmt_free_result(stmt);
} ;

int res = mysql_stmt_fetch(stmt);
if(res == MYSQL_NO_DATA)
return false;
if(is_null)
throw sql_error("null value returned", sql);
if(res != 0)
throw sql_error(mysql_stmt_error(stmt), sql);
mysql_stmt_free_result(stmt);
return true;
}

Expand Down

0 comments on commit 8c3c0ab

Please sign in to comment.