Skip to content

Commit

Permalink
Added locks around all mysql_stmt_init, since they modify list inside
Browse files Browse the repository at this point in the history
MYSQL structure.
  • Loading branch information
lawrinn committed Feb 22, 2017
1 parent 06b5e6a commit 8d85958
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
10 changes: 10 additions & 0 deletions ma_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, char *StmtStr, SQLINTEGER Lengt
MYSQL_STMT *stmt;
char *StmtCopy= NULL;

/* mysql_stmt_init requires locking as well */
LOCK_MARIADB(Stmt->Connection);
stmt= mysql_stmt_init(Stmt->Connection->mariadb);

/* if the entire stmt string passes, we don't have multistatement */
if (stmt && !mysql_stmt_prepare(stmt, StmtStr, Length))
{
mysql_stmt_close(stmt);
UNLOCK_MARIADB(Stmt->Connection);
return 1;
}
mysql_stmt_close(stmt);

/* make sure we don't have trailing whitespace or semicolon */
if (Length)
{
Expand Down Expand Up @@ -159,14 +163,20 @@ unsigned int GetMultiStatements(MADB_Stmt *Stmt, char *StmtStr, SQLINTEGER Lengt
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->MultiStmts[i]);
CloseMultiStatements(Stmt);
if (StmtCopy)
{
my_free(StmtCopy);
}
UNLOCK_MARIADB(Stmt->Connection);

return 0;
}
if (mysql_stmt_param_count(Stmt->MultiStmts[i]) > MaxParams)
MaxParams= mysql_stmt_param_count(Stmt->MultiStmts[i]);
p+= strlen(p) + 1;
++i;
}
UNLOCK_MARIADB(Stmt->Connection);

if (MaxParams)
Stmt->params= (MYSQL_BIND *)MADB_CALLOC(sizeof(MYSQL_BIND) * MaxParams);
}
Expand Down
23 changes: 13 additions & 10 deletions ma_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt)
*pHStmt= Stmt;
Stmt->Connection= Connection;

LOCK_MARIADB(Connection);

if (!(Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb)) ||
!(Stmt->IApd= MADB_DescInit(Connection, MADB_DESC_APD, FALSE)) ||
!(Stmt->IArd= MADB_DescInit(Connection, MADB_DESC_ARD, FALSE)) ||
!(Stmt->IIpd= MADB_DescInit(Connection, MADB_DESC_IPD, FALSE)) ||
!(Stmt->IIrd= MADB_DescInit(Connection, MADB_DESC_IRD, FALSE)))
!(Stmt->IApd= MADB_DescInit(Connection, MADB_DESC_APD, FALSE)) ||
!(Stmt->IArd= MADB_DescInit(Connection, MADB_DESC_ARD, FALSE)) ||
!(Stmt->IIpd= MADB_DescInit(Connection, MADB_DESC_IPD, FALSE)) ||
!(Stmt->IIrd= MADB_DescInit(Connection, MADB_DESC_IRD, FALSE)))
{
goto error;
}

MDBUG_C_PRINT(Stmt->Connection, "-->inited %0x", Stmt->stmt);
mysql_stmt_attr_set(Stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &UpdateMaxLength);

Stmt->Connection= Connection;
Stmt->PutParam= -1;
Stmt->Methods= &MADB_StmtMethods;
/* default behaviour is SQL_CURSOR_STATIC */
Expand All @@ -54,18 +58,16 @@ SQLRETURN MADB_StmtInit(MADB_Dbc *Connection, SQLHANDLE *pHStmt)
Stmt->Ipd= Stmt->IIpd;
Stmt->Ird= Stmt->IIrd;

EnterCriticalSection(&Connection->cs);
Stmt->ListItem.data= (void *)Stmt;
Stmt->Connection->Stmts= list_add(Stmt->Connection->Stmts, &Stmt->ListItem);
LeaveCriticalSection(&Connection->cs);
UNLOCK_MARIADB(Connection);
Stmt->Ard->Header.ArraySize= 1;

return SQL_SUCCESS;

error:
if (Stmt && Stmt->stmt)
{
LOCK_MARIADB(Stmt->Connection);
mysql_stmt_close(Stmt->stmt);
UNLOCK_MARIADB(Stmt->Connection);
}
Expand Down Expand Up @@ -350,7 +352,7 @@ MYSQL_RES* FetchMetadata(MADB_Stmt *Stmt)
}
/* }}} */

/* {{{ MADB_StmtReset - reseting Stmt handler for new use */
/* {{{ MADB_StmtReset - reseting Stmt handler for new use. Has to be called inside a lock */
void MADB_StmtReset(MADB_Stmt *Stmt)
{
/* TODO: These all stuff has to be moved to a separate function, smth like MADB_StmtReset */
Expand Down Expand Up @@ -493,9 +495,10 @@ SQLRETURN MADB_StmtPrepare(MADB_Stmt *Stmt, char *StatementText, SQLINTEGER Text
MADB_SetNativeError(&Stmt->Error, SQL_HANDLE_STMT, Stmt->stmt);
/* We need to close the stmt here, or it becomes unusable like in ODBC-21 */
mysql_stmt_close(Stmt->stmt);
UNLOCK_MARIADB(Stmt->Connection);
Stmt->stmt= mysql_stmt_init(Stmt->Connection->mariadb);

UNLOCK_MARIADB(Stmt->Connection);

return Stmt->Error.ReturnValue;
}
UNLOCK_MARIADB(Stmt->Connection);
Expand Down

0 comments on commit 8d85958

Please sign in to comment.