1
0
mirror of https://github.com/mariadb-corporation/mariadb-connector-c.git synced 2025-08-08 14:02:17 +03:00

Fix for CONC-218:

To prevent unexpected behavior when reusing a statement with mariadb_stmt_execute_direct
a call to mysql_stmt_attr_set with option STMT_ATTR_PREBIND_PARAMS will reset the statement before.
This commit is contained in:
Georg Richter
2016-11-29 13:30:17 +01:00
parent dad2cf6cf4
commit 64536703c7
3 changed files with 94 additions and 10 deletions

View File

@@ -76,6 +76,7 @@ MYSQL_DATA *read_rows(MYSQL *mysql,MYSQL_FIELD *mysql_fields, uint fields);
void free_rows(MYSQL_DATA *cur);
int ma_multi_command(MYSQL *mysql, enum enum_multi_status status);
MYSQL_FIELD * unpack_fields(MYSQL_DATA *data,MA_MEM_ROOT *alloc,uint fields, my_bool default_value, my_bool long_flag_protocol);
static my_bool net_stmt_close(MYSQL_STMT *stmt, my_bool remove);
static my_bool is_not_null= 0;
static my_bool is_null= 1;
@@ -775,15 +776,12 @@ unsigned char* mysql_stmt_execute_generate_request(MYSQL_STMT *stmt, size_t *req
case MYSQL_TYPE_SET:
size+= 5; /* max 8 bytes for size */
if (indicator == STMT_INDICATOR_NTS ||
(!stmt->row_size && stmt->params[i].length[j] == (unsigned long)-1))
(!stmt->row_size && ma_get_length(stmt,i,j) == -1))
size+= strlen(ma_get_buffer_offset(stmt,
stmt->params[i].buffer_type,
stmt->params[i].buffer,j));
else
if (!stmt->row_size)
size+= (size_t)stmt->params[i].length[j];
else
size+= (size_t)*stmt->params[i].length;
size+= (size_t)ma_get_length(stmt, i, j);
break;
default:
size+= mysql_ps_fetch_functions[stmt->params[i].buffer_type].pack_len;
@@ -898,6 +896,13 @@ my_bool STDCALL mysql_stmt_attr_set(MYSQL_STMT *stmt, enum enum_stmt_attr_type a
stmt->prefetch_rows= *(long *)value;
break;
case STMT_ATTR_PREBIND_PARAMS:
if (stmt->state > MYSQL_STMT_INITTED)
{
mysql_stmt_internal_reset(stmt, 1);
net_stmt_close(stmt, 0);
stmt->state= MYSQL_STMT_INITTED;
stmt->params= 0;
}
stmt->prebind_params= *(unsigned int *)value;
break;
case STMT_ATTR_ARRAY_SIZE:

View File

@@ -303,6 +303,12 @@ static int bulk_null(MYSQL *mysql)
unsigned long lengths[2]= {-1, -1};
const char **buf= calloc(1, 2 * sizeof(char *));
if (!bulk_enabled)
{
free(buf);
return SKIP;
}
buf[0]= strdup("foo");
buf[1]= strdup("foobar");
@@ -334,6 +340,7 @@ static int bulk_null(MYSQL *mysql)
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
free(buf);
return OK;
}
@@ -348,6 +355,9 @@ static int bulk5(MYSQL *mysql)
int intval[]= {12,13,14,15,16};
int id[]= {1,2,3,4,5};
if (!bulk_enabled)
return SKIP;
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk5");
check_mysql_rc(rc, mysql);
@@ -403,21 +413,24 @@ static int bulk6(MYSQL *mysql)
int id[]= {1,2,3,4,5};
char indicator[5];
if (!bulk_enabled)
return SKIP;
memset(indicator, STMT_INDICATOR_IGNORE, 5);
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk5");
rc= mysql_query(mysql, "DROP TABLE IF EXISTS bulk6");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "CREATE TABLE bulk5 (a int, b int default 4)");
rc= mysql_query(mysql, "CREATE TABLE bulk6 (a int, b int default 4)");
check_mysql_rc(rc, mysql);
rc= mysql_query(mysql, "INSERT INTO bulk5 VALUES (1,1), (2,2), (3,3), (4,4), (5,5)");
rc= mysql_query(mysql, "INSERT INTO bulk6 VALUES (1,1), (2,2), (3,3), (4,4), (5,5)");
check_mysql_rc(rc, mysql);
memset(bind, 0, sizeof(MYSQL_BIND) * 3);
rc= mysql_stmt_prepare(stmt, "UPDATE bulk5 SET a=?, b=? WHERE a=?", -1);
/* 1st case: UPDATE */
rc= mysql_stmt_prepare(stmt, "UPDATE bulk6 SET a=?, b=? WHERE a=?", -1);
check_stmt_rc(rc, stmt);
bind[0].buffer_type= MYSQL_TYPE_LONG;
@@ -439,7 +452,7 @@ static int bulk6(MYSQL *mysql)
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk5 WHERE a=b+11");
rc= mysql_query(mysql, "SELECT * FROM bulk6 WHERE a=b+11");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
@@ -448,6 +461,40 @@ static int bulk6(MYSQL *mysql)
FAIL_IF(rows != 5, "expected 5 rows");
/* 2nd case: INSERT - ignore indicator should be same as default */
rc= mysql_query(mysql, "DELETE FROM bulk6");
check_mysql_rc(rc, mysql);
stmt= mysql_stmt_init(mysql);
rc= mysql_stmt_prepare(stmt, "INSERT INTO bulk6 VALUES (?,?)", -1);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_attr_set(stmt, STMT_ATTR_ARRAY_SIZE, &array_size);
check_stmt_rc(rc, stmt);
/* this should insert 5 default values (=4) */
memset(indicator, STMT_INDICATOR_DEFAULT, 5);
rc= mysql_stmt_bind_param(stmt, bind);
check_stmt_rc(rc, stmt);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
/* this should insert 5 default values (=4) */
memset(indicator, STMT_INDICATOR_IGNORE, 5);
rc= mysql_stmt_execute(stmt);
check_stmt_rc(rc, stmt);
mysql_stmt_close(stmt);
rc= mysql_query(mysql, "SELECT * FROM bulk6 WHERE b=4");
check_mysql_rc(rc, mysql);
res= mysql_store_result(mysql);
rows= mysql_num_rows(res);
mysql_free_result(res);
FAIL_IF(rows != 10, "expected 10 rows");
return OK;
}

View File

@@ -155,8 +155,40 @@ static int conc_212(MYSQL *mysql)
return OK;
}
static int conc_218(MYSQL *mysql)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
MYSQL_BIND bind[2];
int id=1;
my_bool is_null= 0, error= 0;
unsigned int param_count= 1;
memset(bind, 0, 2 * sizeof(MYSQL_BIND));
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[0].buffer = (void *)&id;
bind[0].buffer_length = 4;
bind[0].is_null = &is_null;
bind[0].error = &error;
mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &param_count);
check_stmt_rc(mysql_stmt_bind_param(stmt, bind), stmt);
check_stmt_rc(mariadb_stmt_execute_direct(stmt, "SELECT ?", -1), stmt);
check_stmt_rc(mysql_stmt_store_result(stmt), stmt);
check_stmt_rc(mysql_stmt_free_result(stmt), stmt);
param_count= 1;
mysql_stmt_attr_set(stmt, STMT_ATTR_PREBIND_PARAMS, &param_count);
check_stmt_rc(mysql_stmt_bind_param(stmt, bind), stmt);
check_stmt_rc(mariadb_stmt_execute_direct(stmt, "SELECT ?", -1), stmt);
mysql_stmt_close(stmt);
return OK;
}
struct my_tests_st my_tests[] = {
{"conc_218", conc_218, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"conc_212", conc_212, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"conc_213", conc_213, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},
{"execute_direct", execute_direct, TEST_CONNECTION_DEFAULT, 0, NULL, NULL},