mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
bug #16017 (memory leaks in embedded server)
There actually was 3 different problems - hash_user_connections wasn't cleaned one strdupped database name wasn't freed and stmt->mem_root wasn't cleaned as it was replased with mysql->field_alloc for result For the last one - i made the library using stmt's fields to store result if it's the case.
This commit is contained in:
@ -47,6 +47,7 @@ C_MODE_START
|
||||
#include "errmsg.h"
|
||||
#include <sql_common.h>
|
||||
|
||||
|
||||
void embedded_get_error(MYSQL *mysql)
|
||||
{
|
||||
THD *thd=(THD *) mysql->thd;
|
||||
@ -66,7 +67,8 @@ void embedded_get_error(MYSQL *mysql)
|
||||
static my_bool
|
||||
emb_advanced_command(MYSQL *mysql, enum enum_server_command command,
|
||||
const char *header, ulong header_length,
|
||||
const char *arg, ulong arg_length, my_bool skip_check)
|
||||
const char *arg, ulong arg_length, my_bool skip_check,
|
||||
MYSQL_STMT *stmt)
|
||||
{
|
||||
my_bool result= 1;
|
||||
THD *thd=(THD *) mysql->thd;
|
||||
@ -90,6 +92,7 @@ emb_advanced_command(MYSQL *mysql, enum enum_server_command command,
|
||||
mysql->affected_rows= ~(my_ulonglong) 0;
|
||||
mysql->field_count= 0;
|
||||
net->last_errno= 0;
|
||||
mysql->current_stmt= stmt;
|
||||
|
||||
thd->store_globals(); // Fix if more than one connect
|
||||
/*
|
||||
@ -183,7 +186,6 @@ static my_bool emb_read_prepare_result(MYSQL *mysql, MYSQL_STMT *stmt)
|
||||
mysql->server_status|= SERVER_STATUS_IN_TRANS;
|
||||
|
||||
stmt->fields= mysql->fields;
|
||||
stmt->mem_root= mysql->field_alloc;
|
||||
mysql->fields= NULL;
|
||||
}
|
||||
|
||||
@ -225,7 +227,7 @@ static int emb_stmt_execute(MYSQL_STMT *stmt)
|
||||
thd->client_param_count= stmt->param_count;
|
||||
thd->client_params= stmt->params;
|
||||
if (emb_advanced_command(stmt->mysql, COM_EXECUTE,0,0,
|
||||
header, sizeof(header), 1) ||
|
||||
header, sizeof(header), 1, stmt) ||
|
||||
emb_mysql_read_query_result(stmt->mysql))
|
||||
{
|
||||
NET *net= &stmt->mysql->net;
|
||||
@ -242,8 +244,6 @@ int emb_read_binary_rows(MYSQL_STMT *stmt)
|
||||
MYSQL_DATA *data;
|
||||
if (!(data= emb_read_rows(stmt->mysql, 0, 0)))
|
||||
return 1;
|
||||
stmt->result= *data;
|
||||
my_free((char *) data, MYF(0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -298,7 +298,8 @@ my_bool emb_next_result(MYSQL *mysql)
|
||||
DBUG_ENTER("emb_next_result");
|
||||
|
||||
if (emb_advanced_command(mysql, COM_QUERY,0,0,
|
||||
thd->query_rest.ptr(),thd->query_rest.length(),1) ||
|
||||
thd->query_rest.ptr(),
|
||||
thd->query_rest.length(),1, 0) ||
|
||||
emb_mysql_read_query_result(mysql))
|
||||
DBUG_RETURN(1);
|
||||
|
||||
@ -482,14 +483,14 @@ void end_embedded_server()
|
||||
} /* extern "C" */
|
||||
|
||||
C_MODE_START
|
||||
void init_embedded_mysql(MYSQL *mysql, int client_flag, char *db)
|
||||
void init_embedded_mysql(MYSQL *mysql, int client_flag)
|
||||
{
|
||||
THD *thd = (THD *)mysql->thd;
|
||||
thd->mysql= mysql;
|
||||
mysql->server_version= server_version;
|
||||
}
|
||||
|
||||
void *create_embedded_thd(int client_flag, char *db)
|
||||
void *create_embedded_thd(int client_flag)
|
||||
{
|
||||
THD * thd= new THD;
|
||||
thd->thread_id= thread_id++;
|
||||
@ -515,8 +516,8 @@ void *create_embedded_thd(int client_flag, char *db)
|
||||
thd->init_for_queries();
|
||||
thd->client_capabilities= client_flag;
|
||||
|
||||
thd->db= db;
|
||||
thd->db_length= db ? strip_sp(db) : 0;
|
||||
thd->db= NULL;
|
||||
thd->db_length= 0;
|
||||
#ifndef NO_EMBEDDED_ACCESS_CHECKS
|
||||
thd->db_access= DB_ACLS;
|
||||
thd->master_access= ~NO_ACCESS;
|
||||
@ -533,18 +534,18 @@ err:
|
||||
}
|
||||
|
||||
#ifdef NO_EMBEDDED_ACCESS_CHECKS
|
||||
int check_embedded_connection(MYSQL *mysql)
|
||||
int check_embedded_connection(MYSQL *mysql, const char *db)
|
||||
{
|
||||
THD *thd= (THD*)mysql->thd;
|
||||
thd->host= (char*)my_localhost;
|
||||
thd->host_or_ip= thd->host;
|
||||
thd->user= my_strdup(mysql->user, MYF(0));
|
||||
thd->priv_user= thd->user;
|
||||
return check_user(thd, COM_CONNECT, NULL, 0, thd->db, true);
|
||||
return check_user(thd, COM_CONNECT, NULL, 0, db, true);
|
||||
}
|
||||
|
||||
#else
|
||||
int check_embedded_connection(MYSQL *mysql)
|
||||
int check_embedded_connection(MYSQL *mysql, const char *db)
|
||||
{
|
||||
THD *thd= (THD*)mysql->thd;
|
||||
int result;
|
||||
@ -578,7 +579,7 @@ int check_embedded_connection(MYSQL *mysql)
|
||||
passwd_len= 0;
|
||||
|
||||
if((result= check_user(thd, COM_CONNECT,
|
||||
scramble_buff, passwd_len, thd->db, true)))
|
||||
scramble_buff, passwd_len, db, true)))
|
||||
goto err;
|
||||
|
||||
return 0;
|
||||
@ -636,8 +637,9 @@ bool Protocol::send_fields(List<Item> *list, uint flag)
|
||||
DBUG_RETURN(0);
|
||||
|
||||
field_count= list->elements;
|
||||
field_alloc= &mysql->field_alloc;
|
||||
if (!(client_field= thd->mysql->fields=
|
||||
field_alloc= mysql->current_stmt ? &mysql->current_stmt->mem_root :
|
||||
&mysql->field_alloc;
|
||||
if (!(client_field= mysql->fields=
|
||||
(MYSQL_FIELD *)alloc_root(field_alloc,
|
||||
sizeof(MYSQL_FIELD) * field_count)))
|
||||
goto err;
|
||||
@ -707,7 +709,7 @@ bool Protocol::send_fields(List<Item> *list, uint flag)
|
||||
client_field->max_length= 0;
|
||||
++client_field;
|
||||
}
|
||||
thd->mysql->field_count= field_count;
|
||||
mysql->field_count= field_count;
|
||||
|
||||
DBUG_RETURN(prepare_for_send(list));
|
||||
err:
|
||||
@ -736,13 +738,20 @@ bool Protocol_prep::write()
|
||||
|
||||
if (!data)
|
||||
{
|
||||
if (!(data= (MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
|
||||
MYF(MY_WME | MY_ZEROFILL))))
|
||||
return true;
|
||||
MYSQL *mysql= thd->mysql;
|
||||
|
||||
if (mysql->current_stmt)
|
||||
data= &mysql->current_stmt->result;
|
||||
else
|
||||
{
|
||||
if (!(data= (MYSQL_DATA*) my_malloc(sizeof(MYSQL_DATA),
|
||||
MYF(MY_WME | MY_ZEROFILL))))
|
||||
return true;
|
||||
|
||||
init_alloc_root(&data->alloc,8192,0); /* Assume rowlength < 8192 */
|
||||
data->alloc.min_malloc=sizeof(MYSQL_ROWS);
|
||||
}
|
||||
alloc= &data->alloc;
|
||||
init_alloc_root(alloc,8192,0); /* Assume rowlength < 8192 */
|
||||
alloc->min_malloc=sizeof(MYSQL_ROWS);
|
||||
data->rows=0;
|
||||
data->fields=field_count;
|
||||
data->prev_ptr= &data->data;
|
||||
|
Reference in New Issue
Block a user