From 94f215cbe1d75b5eaf88f0e022a53918a36609a5 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 10 Feb 2004 16:58:20 +0300 Subject: [PATCH] Fixed bug #2248 "mysql_fetch without prior mysql_execute hangs" Done clean-up in prep stmt API functions: 1) Removed some checks that were performed only in debug version were making debug version more tolerable to user errors than production (and thus caused problems for example masking some bugs). 2) Also removed some other checks to make prep stmt API consistent with the rest of C API (this also in line with general politics - make checks in only those places where errors are very common and hard to spot). include/mysql.h: Removed CHECK_EXTRA_ARGUMENTS define since it is no longer used anywhere. libmysql/libmysql.c: Added check that will cause mysql_fetch() to bark then it is used without calling mysql_execute() before. Removed checks that were performed only in debug version and caused problems since they were making debug version more tolerable to user errors than production. Also removed some other checks to make prep stmt API consistent in this regard with the rest of C API (this also in line with general politics - make checks in only those places where errors are very common and hard to spot). tests/client_test.c: Updated tests to reflect removal of some checks in prep stmt API. Removed lines that caused bug #2473 to pop up, should be added as separate test with the fix for this bug. Added test for bug#2248 "mysql_fetch without prior mysql_execute hangs" --- include/mysql.h | 3 -- libmysql/libmysql.c | 70 +++++--------------------------- tests/client_test.c | 98 +++++++++++++++++++++++++++++++-------------- 3 files changed, 79 insertions(+), 92 deletions(-) diff --git a/include/mysql.h b/include/mysql.h index d1b467f9649..ebf3928ce9c 100644 --- a/include/mysql.h +++ b/include/mysql.h @@ -57,9 +57,6 @@ typedef int my_socket; #include "mysql_com.h" #include "mysql_version.h" #include "typelib.h" -#ifndef DBUG_OFF -#define CHECK_EXTRA_ARGUMENTS -#endif #include "my_list.h" /* for LISTs used in 'MYSQL' and 'MYSQL_STMT' */ diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c index 977657f8998..829ee09090e 100644 --- a/libmysql/libmysql.c +++ b/libmysql/libmysql.c @@ -1653,14 +1653,6 @@ mysql_prepare(MYSQL *mysql, const char *query, ulong length) DBUG_ENTER("mysql_prepare"); DBUG_ASSERT(mysql != 0); -#ifdef CHECK_EXTRA_ARGUMENTS - if (!query) - { - set_mysql_error(mysql, CR_NULL_POINTER, unknown_sqlstate); - DBUG_RETURN(0); - } -#endif - if (!(stmt= (MYSQL_STMT *) my_malloc(sizeof(MYSQL_STMT), MYF(MY_WME | MY_ZEROFILL))) || !(stmt->query= my_strdup_with_length((byte *) query, length, MYF(0)))) @@ -2086,19 +2078,6 @@ int STDCALL mysql_execute(MYSQL_STMT *stmt) { DBUG_ENTER("mysql_execute"); - if (stmt->state == MY_ST_UNKNOWN) - { - set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate); - DBUG_RETURN(1); - } -#ifdef CHECK_EXTRA_ARGUMENTS - if (stmt->param_count && !stmt->param_buffers) - { - /* Parameters exists, but no bound buffers */ - set_stmt_error(stmt, CR_NOT_ALL_PARAMS_BOUND, unknown_sqlstate); - DBUG_RETURN(1); - } -#endif if ((*stmt->mysql->methods->stmt_execute)(stmt)) DBUG_RETURN(1); @@ -2144,19 +2123,6 @@ my_bool STDCALL mysql_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind) MYSQL_BIND *param, *end; DBUG_ENTER("mysql_bind_param"); -#ifdef CHECK_EXTRA_ARGUMENTS - if (stmt->state == MY_ST_UNKNOWN) - { - set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate); - DBUG_RETURN(1); - } - if (!stmt->param_count) - { - set_stmt_error(stmt, CR_NO_PARAMETERS_EXISTS, unknown_sqlstate); - DBUG_RETURN(1); - } -#endif - /* Allocated on prepare */ memcpy((char*) stmt->params, (char*) bind, sizeof(MYSQL_BIND) * stmt->param_count); @@ -2279,11 +2245,6 @@ mysql_send_long_data(MYSQL_STMT *stmt, uint param_number, DBUG_PRINT("enter",("param no : %d, data : %lx, length : %ld", param_number, data, length)); - if (param_number >= stmt->param_count) - { - set_stmt_error(stmt, CR_INVALID_PARAMETER_NO, unknown_sqlstate); - DBUG_RETURN(1); - } param= stmt->params+param_number; if (param->buffer_type < MYSQL_TYPE_TINY_BLOB || param->buffer_type > MYSQL_TYPE_STRING) @@ -2853,18 +2814,6 @@ my_bool STDCALL mysql_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind) DBUG_ENTER("mysql_bind_result"); DBUG_ASSERT(stmt != 0); -#ifdef CHECK_EXTRA_ARGUMENTS - if (stmt->state == MY_ST_UNKNOWN) - { - set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate); - DBUG_RETURN(1); - } - if (!bind) - { - set_stmt_error(stmt, CR_NULL_POINTER, unknown_sqlstate); - DBUG_RETURN(1); - } -#endif if (!(bind_count= stmt->field_count) && !(bind_count= alloc_stmt_fields(stmt))) DBUG_RETURN(0); @@ -3035,6 +2984,15 @@ int STDCALL mysql_fetch(MYSQL_STMT *stmt) } else /* un-buffered */ { + if (mysql->status != MYSQL_STATUS_GET_RESULT) + { + if (!stmt->field_count) + goto no_data; + + set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate); + DBUG_RETURN(1); + } + if((*mysql->methods->unbuffered_fetch)(mysql, ( char **)&row)) { set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, @@ -3065,7 +3023,7 @@ no_data: mysql_fetch_column() stmt Prepared statement handler bind Where data should be placed. Should be filled in as - when calling mysql_bind_param() + when calling mysql_bind_result() column Column to fetch (first column is 0) ulong offset Offset in result data (to fetch blob in pieces) This is normally 0 @@ -3083,14 +3041,6 @@ int STDCALL mysql_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, if (!stmt->current_row) goto no_data; -#ifdef CHECK_EXTRA_ARGUMENTS - if (column >= stmt->field_count) - { - set_stmt_errmsg(stmt, "Invalid column descriptor",1, unknown_sqlstate); - DBUG_RETURN(1); - } -#endif - if (param->null_field) { if (bind->is_null) diff --git a/tests/client_test.c b/tests/client_test.c index 66637dcb04b..992c0ce5e78 100644 --- a/tests/client_test.c +++ b/tests/client_test.c @@ -953,8 +953,6 @@ static void test_prepare_simple() verify_param_count(stmt,1); - rc = mysql_execute(stmt); - mystmt_r(stmt, rc); mysql_stmt_close(stmt); /* select */ @@ -5589,27 +5587,24 @@ static void test_pure_coverage() stmt = mysql_prepare(mysql,"insert into test_pure(c67788) values(10)",100); mystmt_init_r(stmt); -#ifndef DBUG_OFF - stmt = mysql_prepare(mysql,(const char *)0,0); - mystmt_init_r(stmt); - + /* Query without params and result should allow to bind 0 arrays */ stmt = mysql_prepare(mysql,"insert into test_pure(c2) values(10)",100); mystmt_init(stmt); + + rc = mysql_bind_param(stmt, (MYSQL_BIND*)0); + mystmt(stmt, rc); + + rc = mysql_execute(stmt); + mystmt(stmt, rc); - rc = mysql_bind_param(stmt, bind); - mystmt_r(stmt, rc); - + rc = mysql_bind_result(stmt, (MYSQL_BIND*)0); + mystmt(stmt, rc); + mysql_stmt_close(stmt); -#endif stmt = mysql_prepare(mysql,"insert into test_pure(c2) values(?)",100); mystmt_init(stmt); -#ifndef DBUG_OFF - rc = mysql_execute(stmt); - mystmt_r(stmt, rc);/* No parameters supplied */ -#endif - bind[0].length= &length; bind[0].is_null= 0; bind[0].buffer_length= 0; @@ -5622,9 +5617,6 @@ static void test_pure_coverage() rc = mysql_bind_param(stmt, bind); mystmt(stmt, rc); - rc = mysql_send_long_data(stmt, 20, (char *)"venu", 4); - mystmt_r(stmt, rc); /* wrong param number */ - rc = mysql_stmt_store_result(stmt); mystmt(stmt, rc); @@ -5636,14 +5628,9 @@ static void test_pure_coverage() rc = mysql_execute(stmt); mystmt(stmt, rc); -#ifndef DBUG_OFF - rc = mysql_bind_result(stmt, (MYSQL_BIND *)0); - mystmt_r(stmt, rc); - bind[0].buffer_type= MYSQL_TYPE_GEOMETRY; rc = mysql_bind_result(stmt, bind); mystmt_r(stmt, rc); /* unsupported buffer type */ -#endif rc = mysql_stmt_store_result(stmt); mystmt(stmt, rc); @@ -7291,9 +7278,6 @@ static void test_fetch_offset() rc = mysql_fetch(stmt); mystmt(stmt,rc); - rc = mysql_fetch_column(stmt,bind,4,0); - mystmt_r(stmt,rc); - data[0]= '\0'; rc = mysql_fetch_column(stmt,bind,0,0); mystmt(stmt,rc); @@ -7411,9 +7395,6 @@ static void test_fetch_column() fprintf(stdout, "\n col 0: %d(%ld)", c1, l1); assert(c1 == 1 && l1 == 4); - rc = mysql_fetch_column(stmt,bind,10,0); - mystmt_r(stmt,rc); - rc = mysql_fetch(stmt); mystmt(stmt,rc); @@ -8096,6 +8077,64 @@ static void test_bug1946() } +/* + Test for bug#2248 "mysql_fetch without prior mysql_execute hangs" +*/ + +static void test_bug2248() +{ + MYSQL_STMT *stmt; + int rc; + const char *query1= "SELECT DATABASE()"; + const char *query2= "INSERT INTO test_bug2248 VALUES (10)"; + + myheader("test_bug2248"); + + rc= mysql_query(mysql, "DROP TABLE IF EXISTS test_bug2248"); + myquery(rc); + + rc= mysql_query(mysql, "CREATE TABLE test_bug2248 (id int)"); + myquery(rc); + + stmt= mysql_prepare(mysql, query1, strlen(query1)); + mystmt_init(stmt); + + /* This should not hang */ + rc= mysql_fetch(stmt); + mystmt_r(stmt,rc); + + /* And this too */ + rc= mysql_stmt_store_result(stmt); + mystmt_r(stmt,rc); + + mysql_stmt_close(stmt); + + stmt= mysql_prepare(mysql, query2, strlen(query2)); + mystmt_init(stmt); + + rc= mysql_execute(stmt); + mystmt(stmt,rc); + + /* This too should not hang but should return proper error */ + rc= mysql_fetch(stmt); + assert(rc==MYSQL_NO_DATA); + + /* This too should not hang but should not bark */ + rc= mysql_stmt_store_result(stmt); + mystmt(stmt,rc); + + /* This should return proper error */ + rc= mysql_fetch(stmt); + mystmt_r(stmt,rc); + assert(rc==MYSQL_NO_DATA); + + mysql_stmt_close(stmt); + + rc= mysql_query(mysql,"DROP TABLE test_bug2248"); + myquery(rc); +} + + /* Read and parse arguments and MySQL options from my.cnf */ @@ -8340,6 +8379,7 @@ int main(int argc, char **argv) test_bug1644(); /* BUG#1644 */ test_bug1946(); /* test that placeholders are allowed only in prepared queries */ + test_bug2248(); /* BUG#2248 */ end_time= time((time_t *)0); total_time+= difftime(end_time, start_time);