2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-09-01 14:35:29 +00:00

[#2792] Check that mysql pointer is non-null

This is a workaround for the libmysqlclient that dereferences mysql ptr
in the MYSQL_STMT after reconnect. Kea checks that this pointer is not
NULL before using the statement.
This commit is contained in:
Marcin Siodelski
2023-07-17 19:17:30 +02:00
parent 53f3d241c8
commit d3971afa6e
6 changed files with 91 additions and 75 deletions

View File

@@ -2081,7 +2081,7 @@ private:
" - invalid statement index" << statement_index_);
}
statement_ = conn_.statements_[statement_index_];
statement_ = conn_.getStatement(statement_index_);
}
/// @brief Database connection to use to execute the query
@@ -2366,11 +2366,11 @@ MySqlLeaseMgr::addLeaseCommon(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
std::vector<MYSQL_BIND>& bind) {
// Bind the parameters to the statement
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], &bind[0]);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), &bind[0]);
checkError(ctx, status, stindex, "unable to bind parameters");
// Execute the statement
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
if (status != 0) {
// Failure: check for the special case of duplicate entry. If this is
@@ -2484,31 +2484,31 @@ MySqlLeaseMgr::getLeaseCollection(MySqlLeaseContextPtr& ctx,
if (bind) {
// Bind the selection parameters to the statement
status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
}
// Set up the MYSQL_BIND array for the data being returned and bind it to
// the statement.
std::vector<MYSQL_BIND> outbind = exchange->createBindForReceive();
status = mysql_stmt_bind_result(ctx->conn_.statements_[stindex], &outbind[0]);
status = mysql_stmt_bind_result(ctx->conn_.getStatement(stindex), &outbind[0]);
checkError(ctx, status, stindex, "unable to bind SELECT clause parameters");
// Execute the statement
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
// Ensure that all the lease information is retrieved in one go to avoid
// overhead of going back and forth between client and server.
status = mysql_stmt_store_result(ctx->conn_.statements_[stindex]);
status = mysql_stmt_store_result(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to set up for storing all results");
// Set up the fetch "release" object to release resources associated
// with the call to mysql_stmt_fetch when this method exits, then
// retrieve the data.
MySqlFreeResult fetch_release(ctx->conn_.statements_[stindex]);
MySqlFreeResult fetch_release(ctx->conn_.getStatement(stindex));
int count = 0;
while ((status = mysql_stmt_fetch(ctx->conn_.statements_[stindex])) == 0) {
while ((status = mysql_stmt_fetch(ctx->conn_.getStatement(stindex))) == 0) {
try {
result.push_back(exchange->getLeaseData());
@@ -3241,16 +3241,16 @@ MySqlLeaseMgr::updateLeaseCommon(MySqlLeaseContextPtr& ctx,
const LeasePtr& lease) {
// Bind the parameters to the statement
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind parameters");
// Execute
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
// See how many rows were affected. The statement should only update a
// single row.
int affected_rows = mysql_stmt_affected_rows(ctx->conn_.statements_[stindex]);
int affected_rows = mysql_stmt_affected_rows(ctx->conn_.getStatement(stindex));
// Check success case first as it is the most likely outcome.
if (affected_rows == 1) {
@@ -3412,16 +3412,16 @@ MySqlLeaseMgr::deleteLeaseCommon(MySqlLeaseContextPtr& ctx,
StatementIndex stindex,
MYSQL_BIND* bind) {
// Bind the input parameters to the statement
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
// Execute
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
// See how many rows were affected. Note that the statement may delete
// multiple rows.
return (static_cast<uint64_t>(mysql_stmt_affected_rows(ctx->conn_.statements_[stindex])));
return (static_cast<uint64_t>(mysql_stmt_affected_rows(ctx->conn_.getStatement(stindex))));
}
bool
@@ -3903,11 +3903,11 @@ MySqlLeaseMgr::deleteRelayId6(const IOAddress& addr) {
StatementIndex stindex = DELETE_RELAY_ID6;
// Bind the input parameters to the statement.
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
// Execute.
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
}
@@ -3933,11 +3933,11 @@ MySqlLeaseMgr::deleteRemoteId6(const IOAddress& addr) {
StatementIndex stindex = DELETE_REMOTE_ID6;
// Bind the input parameters to the statement.
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
// Execute.
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
}
@@ -3977,11 +3977,11 @@ MySqlLeaseMgr::addRelayId6(const IOAddress& lease_addr,
StatementIndex stindex = ADD_RELAY_ID6;
// Bind the input parameters to the statement.
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
// Execute.
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
}
@@ -4021,11 +4021,11 @@ MySqlLeaseMgr::addRemoteId6(const IOAddress& lease_addr,
StatementIndex stindex = ADD_REMOTE_ID6;
// Bind the input parameters to the statement.
int status = mysql_stmt_bind_param(ctx->conn_.statements_[stindex], bind);
int status = mysql_stmt_bind_param(ctx->conn_.getStatement(stindex), bind);
checkError(ctx, status, stindex, "unable to bind WHERE clause parameter");
// Execute.
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to execute");
}
@@ -4807,13 +4807,13 @@ MySqlLeaseMgr::wipeExtendedInfoTables6() {
MySqlLeaseContextPtr ctx = get_context.ctx_;
StatementIndex stindex = WIPE_RELAY_ID6;
int status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
int status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
if (status != 0) {
checkError(ctx, status, stindex, "unable to execute");
}
stindex = WIPE_REMOTE_ID6;
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
if (status != 0) {
checkError(ctx, status, stindex, "unable to execute");
}
@@ -4835,22 +4835,22 @@ MySqlLeaseMgr::byRelayId6size() const {
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = reinterpret_cast<char*>(&count);
int status = mysql_stmt_bind_result(ctx->conn_.statements_[stindex], &bind[0]);
int status = mysql_stmt_bind_result(ctx->conn_.getStatement(stindex), &bind[0]);
checkError(ctx, status, stindex, "unable to bind SELECT clause parameters");
// Execute.
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
if (status != 0) {
checkError(ctx, status, stindex, "unable to execute");
}
status = mysql_stmt_store_result(ctx->conn_.statements_[stindex]);
status = mysql_stmt_store_result(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to store result");
// Fetch the result.
MySqlFreeResult fetch_release(ctx->conn_.statements_[stindex]);
MySqlFreeResult fetch_release(ctx->conn_.getStatement(stindex));
status = mysql_stmt_fetch(ctx->conn_.statements_[stindex]);
status = mysql_stmt_fetch(ctx->conn_.getStatement(stindex));
if (status != 0) {
checkError(ctx, status, stindex, "unable to fetch results");
}
@@ -4873,22 +4873,22 @@ MySqlLeaseMgr::byRemoteId6size() const {
bind[0].buffer_type = MYSQL_TYPE_LONGLONG;
bind[0].buffer = reinterpret_cast<char*>(&count);
int status = mysql_stmt_bind_result(ctx->conn_.statements_[stindex], &bind[0]);
int status = mysql_stmt_bind_result(ctx->conn_.getStatement(stindex), &bind[0]);
checkError(ctx, status, stindex, "unable to bind SELECT clause parameters");
// Execute.
status = MysqlExecuteStatement(ctx->conn_.statements_[stindex]);
status = MysqlExecuteStatement(ctx->conn_.getStatement(stindex));
if (status != 0) {
checkError(ctx, status, stindex, "unable to execute");
}
status = mysql_stmt_store_result(ctx->conn_.statements_[stindex]);
status = mysql_stmt_store_result(ctx->conn_.getStatement(stindex));
checkError(ctx, status, stindex, "unable to store result");
// Fetch the result.
MySqlFreeResult fetch_release(ctx->conn_.statements_[stindex]);
MySqlFreeResult fetch_release(ctx->conn_.getStatement(stindex));
status = mysql_stmt_fetch(ctx->conn_.statements_[stindex]);
status = mysql_stmt_fetch(ctx->conn_.getStatement(stindex));
if (status != 0) {
checkError(ctx, status, stindex, "unable to fetch results");
}