Harden MySQL locking (fixes #1207) (#2528)

This commit is contained in:
Nicholas Hastings
2026-08-01 18:29:14 +00:00
committed by GitHub
parent 9faf4aaa85
commit 1033441f22
3 changed files with 33 additions and 0 deletions
@@ -332,6 +332,8 @@ unsigned int MyQuery::GetAffectedRows()
bool MyQuery::FetchMoreResults()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
if (m_rs.m_pRes == NULL)
{
return false;
@@ -355,6 +357,8 @@ bool MyQuery::FetchMoreResults()
void MyQuery::Destroy()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
/* :TODO: All this rot should be moved into the destructor,
* and the Update() function needs to not be so stupid.
*/
+17
View File
@@ -108,6 +108,8 @@ MyDatabase::MyDatabase(MYSQL *mysql, const DatabaseInfo *info, bool persistent)
MyDatabase::~MyDatabase()
{
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
/* Remove us from the search list */
if (m_bPersistent)
g_MyDriver.RemoveFromList(this, true);
@@ -131,16 +133,20 @@ const DatabaseInfo &MyDatabase::GetInfo()
unsigned int MyDatabase::GetInsertID()
{
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
return (unsigned int)mysql_insert_id(m_mysql);
}
unsigned int MyDatabase::GetAffectedRows()
{
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
return (unsigned int)mysql_affected_rows(m_mysql);
}
const char *MyDatabase::GetError(int *errCode)
{
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
if (errCode)
{
*errCode = mysql_errno(m_mysql);
@@ -151,6 +157,8 @@ const char *MyDatabase::GetError(int *errCode)
bool MyDatabase::QuoteString(const char *str, char buffer[], size_t maxlength, size_t *newSize)
{
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
unsigned long size = static_cast<unsigned long>(strlen(str));
unsigned long needed = size * 2 + 1;
@@ -185,6 +193,10 @@ bool MyDatabase::DoSimpleQuery(const char *query)
IQuery *MyDatabase::DoQuery(const char *query)
{
// A MYSQL connection may be shared between threads only if the entire
// mysql_real_query()/mysql_store_result() sequence is serialized.
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
if (mysql_real_query(m_mysql, query, static_cast<unsigned long>(strlen(query))) != 0)
{
return NULL;
@@ -216,6 +228,9 @@ bool MyDatabase::DoSimpleQueryEx(const char *query, size_t len)
IQuery *MyDatabase::DoQueryEx(const char *query, size_t len)
{
// See DoQuery().
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
if (mysql_real_query(m_mysql, query, static_cast<unsigned long>(len)) != 0)
{
return NULL;
@@ -246,6 +261,8 @@ unsigned int MyDatabase::GetInsertIDForQuery(IQuery *query)
IPreparedQuery *MyDatabase::PrepareQuery(const char *query, char *error, size_t maxlength, int *errCode)
{
std::lock_guard<std::recursive_mutex> lock(m_FullLock);
MYSQL_STMT *stmt = mysql_stmt_init(m_mysql);
if (!stmt)
{
+12
View File
@@ -35,6 +35,8 @@
MyStatement::MyStatement(MyDatabase *db, MYSQL_STMT *stmt)
: m_mysql(db->m_mysql), m_pParent(db), m_stmt(stmt), m_pRes(NULL), m_rs(NULL), m_Results(false)
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
m_Params = (unsigned int)mysql_stmt_param_count(m_stmt);
if (m_Params)
@@ -53,6 +55,8 @@ MyStatement::MyStatement(MyDatabase *db, MYSQL_STMT *stmt)
MyStatement::~MyStatement()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
while (FetchMoreResults())
{
/* Spin until all are gone */
@@ -97,6 +101,8 @@ void MyStatement::ClearResults()
bool MyStatement::FetchMoreResults()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
if (m_pRes == NULL)
{
return false;
@@ -274,6 +280,8 @@ bool MyStatement::BindParamNull(unsigned int param)
bool MyStatement::Execute()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
/* Clear any past result first! */
while (FetchMoreResults())
{
@@ -336,6 +344,8 @@ bool MyStatement::Execute()
const char *MyStatement::GetError(int *errCode/* =NULL */)
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
if (errCode)
{
*errCode = mysql_stmt_errno(m_stmt);
@@ -346,11 +356,13 @@ const char *MyStatement::GetError(int *errCode/* =NULL */)
unsigned int MyStatement::GetAffectedRows()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
return (unsigned int)mysql_stmt_affected_rows(m_stmt);
}
unsigned int MyStatement::GetInsertID()
{
std::lock_guard<std::recursive_mutex> lock(m_pParent->m_FullLock);
return (unsigned int)mysql_stmt_insert_id(m_stmt);
}