From 67108bdfa781a278aee92f1a6118116931b9e10f Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Mar 2004 12:53:27 +0200 Subject: [PATCH 01/12] Optimizations mysql-test/r/create.result: Changed ERROR -> NOTE (for enum/set) sql/lock.cc: Removed not needed test (table_list->db is always set) sql/sql_repl.cc: Removed not needed test sql/sql_table.cc: ERROR -> NOTE Removed not needed test strings/strtod.c: Portabilty fix. (DBL_MAX *10 is not safe) --- mysql-test/r/create.result | 16 ++++++++-------- sql/lock.cc | 2 +- sql/sql_repl.cc | 2 -- sql/sql_table.cc | 5 +---- strings/strtod.c | 10 ++++++---- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/mysql-test/r/create.result b/mysql-test/r/create.result index b65c8b284b3..c3043854063 100644 --- a/mysql-test/r/create.result +++ b/mysql-test/r/create.result @@ -459,16 +459,16 @@ drop table t1, t2; create table t1(cenum enum('a'), cset set('b')); create table t2(cenum enum('a','a'), cset set('b','b')); Warnings: -Error 1291 Column 'cenum' has duplicated value 'a' in ENUM -Error 1291 Column 'cset' has duplicated value 'b' in SET +Note 1291 Column 'cenum' has duplicated value 'a' in ENUM +Note 1291 Column 'cset' has duplicated value 'b' in SET create table t3(cenum enum('a','A','a','c','c'), cset set('b','B','b','d','d')); Warnings: -Error 1291 Column 'cenum' has duplicated value 'a' in ENUM -Error 1291 Column 'cenum' has duplicated value 'A' in ENUM -Error 1291 Column 'cenum' has duplicated value 'c' in ENUM -Error 1291 Column 'cset' has duplicated value 'b' in SET -Error 1291 Column 'cset' has duplicated value 'B' in SET -Error 1291 Column 'cset' has duplicated value 'd' in SET +Note 1291 Column 'cenum' has duplicated value 'a' in ENUM +Note 1291 Column 'cenum' has duplicated value 'A' in ENUM +Note 1291 Column 'cenum' has duplicated value 'c' in ENUM +Note 1291 Column 'cset' has duplicated value 'b' in SET +Note 1291 Column 'cset' has duplicated value 'B' in SET +Note 1291 Column 'cset' has duplicated value 'd' in SET drop table t1, t2, t3; create database test_$1; use test_$1; diff --git a/sql/lock.cc b/sql/lock.cc index 29795415720..67a085ff708 100644 --- a/sql/lock.cc +++ b/sql/lock.cc @@ -495,7 +495,7 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list) { TABLE *table; char key[MAX_DBKEY_LENGTH]; - char *db= table_list->db ? table_list->db : (thd->db ? thd->db : (char*) ""); + char *db= table_list->db; uint key_length; DBUG_ENTER("lock_table_name"); safe_mutex_assert_owner(&LOCK_open); diff --git a/sql/sql_repl.cc b/sql/sql_repl.cc index 8a5a9564d3e..32c5f0bfdab 100644 --- a/sql/sql_repl.cc +++ b/sql/sql_repl.cc @@ -662,8 +662,6 @@ err: int start_slave(THD* thd , MASTER_INFO* mi, bool net_report) { int slave_errno= 0; - if (!thd) - thd = current_thd; int thread_mask; DBUG_ENTER("start_slave"); diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 9d1855ef936..05d9a5f706f 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -365,9 +365,6 @@ void check_duplicates_in_interval(const char *set_or_name, unsigned int old_count= typelib->count; const char **old_type_names= typelib->type_names; - if (typelib->count <= 1) - return; - old_count= typelib->count; old_type_names= typelib->type_names; const char **cur_value= typelib->type_names; @@ -377,7 +374,7 @@ void check_duplicates_in_interval(const char *set_or_name, typelib->count--; if (find_type((char*)*cur_value,typelib,1)) { - push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_ERROR, + push_warning_printf(current_thd,MYSQL_ERROR::WARN_LEVEL_NOTE, ER_DUPLICATED_VALUE_IN_TYPE, ER(ER_DUPLICATED_VALUE_IN_TYPE), name,*cur_value,set_or_name); diff --git a/strings/strtod.c b/strings/strtod.c index a06b74248cb..4c3f31bc00c 100644 --- a/strings/strtod.c +++ b/strings/strtod.c @@ -36,6 +36,11 @@ static double scaler1[] = { 1.0, 10.0, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9 }; + +#ifndef HUGE_VAL /* Should be +Infinitive */ +#define HUGE_VAL DBL_MAX +#endif + double my_strtod(const char *str, char **end) { double result= 0.0; @@ -90,10 +95,7 @@ double my_strtod(const char *str, char **end) } if (exp >= 1000) { - if (neg) - result= 0.0; - else - result= DBL_MAX*10; + result= neg ? 0.0 : HUGE_VAL; goto done; } while (exp >= 100) From 60354533c1156472d5433e15dd295ee76a9b8ee5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Mar 2004 19:18:30 +0400 Subject: [PATCH 02/12] Fix for bug#2972 Problem the the CONV() function --- mysql-test/r/func_str.result | 3 +++ mysql-test/t/func_str.test | 7 +++++++ strings/ctype-simple.c | 16 ++++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/mysql-test/r/func_str.result b/mysql-test/r/func_str.result index eb09a746a1c..86c4b8cf0dc 100644 --- a/mysql-test/r/func_str.result +++ b/mysql-test/r/func_str.result @@ -624,6 +624,9 @@ Note 1003 select high_priority md5(_latin1'hello') AS `md5('hello')`,sha(_latin1 SELECT lpad(12345, 5, "#"); lpad(12345, 5, "#") 12345 +SELECT conv(71, 10, 36), conv('1Z', 36, 10); +conv(71, 10, 36) conv('1Z', 36, 10) +1Z 71 create table t1 (id int(1), str varchar(10)) DEFAULT CHARSET=utf8; insert into t1 values (1,'aaaaaaaaaa'), (2,'bbbbbbbbbb'); create table t2 (id int(1), str varchar(10)) DEFAULT CHARSET=utf8; diff --git a/mysql-test/t/func_str.test b/mysql-test/t/func_str.test index c9ead51c0a6..1e6d279f721 100644 --- a/mysql-test/t/func_str.test +++ b/mysql-test/t/func_str.test @@ -361,6 +361,13 @@ explain extended select md5('hello'), sha('abc'), sha1('abc'), soundex(''), 'moo SELECT lpad(12345, 5, "#"); +# +# Bug #2972 +# + +SELECT conv(71, 10, 36), conv('1Z', 36, 10); + + # # Bug #3089 diff --git a/strings/ctype-simple.c b/strings/ctype-simple.c index 719ae14f653..6e7d0b849e2 100644 --- a/strings/ctype-simple.c +++ b/strings/ctype-simple.c @@ -262,9 +262,9 @@ long my_strntol_8bit(CHARSET_INFO *cs, { if (c>='0' && c<='9') c -= '0'; - else if (c>='A' && c<='F') + else if (c>='A' && c<='Z') c = c - 'A' + 10; - else if (c>='a' && c<='f') + else if (c>='a' && c<='z') c = c - 'a' + 10; else break; @@ -384,9 +384,9 @@ ulong my_strntoul_8bit(CHARSET_INFO *cs, { if (c>='0' && c<='9') c -= '0'; - else if (c>='A' && c<='F') + else if (c>='A' && c<='Z') c = c - 'A' + 10; - else if (c>='a' && c<='f') + else if (c>='a' && c<='z') c = c - 'a' + 10; else break; @@ -499,9 +499,9 @@ longlong my_strntoll_8bit(CHARSET_INFO *cs __attribute__((unused)), { if (c>='0' && c<='9') c -= '0'; - else if (c>='A' && c<='F') + else if (c>='A' && c<='Z') c = c - 'A' + 10; - else if (c>='a' && c<='f') + else if (c>='a' && c<='z') c = c - 'a' + 10; else break; @@ -622,9 +622,9 @@ ulonglong my_strntoull_8bit(CHARSET_INFO *cs, { if (c>='0' && c<='9') c -= '0'; - else if (c>='A' && c<='F') + else if (c>='A' && c<='Z') c = c - 'A' + 10; - else if (c>='a' && c<='f') + else if (c>='a' && c<='z') c = c - 'a' + 10; else break; From 5148b005111200b45672f1de30a16f7d902cc8ca Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Mar 2004 19:47:51 +0400 Subject: [PATCH 03/12] Fix for bug#3041 --safe-updates option declaration anomaly --- client/mysql.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/mysql.cc b/client/mysql.cc index 5d69d3838ee..82925400c44 100644 --- a/client/mysql.cc +++ b/client/mysql.cc @@ -619,10 +619,10 @@ static struct my_option my_long_options[] = (gptr*) ¤t_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, #endif {"safe-updates", 'U', "Only allow UPDATE and DELETE that uses keys.", - (gptr*) &safe_updates, (gptr*) &safe_updates, 0, GET_BOOL, OPT_ARG, 0, 0, + (gptr*) &safe_updates, (gptr*) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"i-am-a-dummy", 'U', "Synonym for option --safe-updates, -U.", - (gptr*) &safe_updates, (gptr*) &safe_updates, 0, GET_BOOL, OPT_ARG, 0, 0, + (gptr*) &safe_updates, (gptr*) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, {"verbose", 'v', "Write more. (-v -v -v gives the table output format).", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, From ef44fb9af3ee729de5897f290c13437bbbc09da5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Mar 2004 20:20:47 +0300 Subject: [PATCH 04/12] Fixes for bugs #2274 "mysqld gets SIGSEGV during processing of malformed COM_EXECUTE packet" and #2795 "prepare + execute without bind_param crashes server" and #2473 "seg fault running tests/client_test.c": - length checking added to packet parser - default impelemntation of Item_param::set_param_func will work in case of malformed packet. No test cases are possible in our test suite, as there are no tests operating on protocol layer. sql/item.cc: Default set_param function implemented: this is to not sigsegv in case of malformed packet with no parameters data. sql/item.h: - Item_param constructor moved to .cc to be able to assign set_param_func. - now embedded and ordinary versions of set_param have the same signature. sql/mysql_priv.h: mysql_stmt_execute now requires packet_length sql/sql_parse.cc: mysql_stmt_execute now requires packet length. sql/sql_prepare.cc: - length checking added to all functions working with network packet. - set_param_func's in embedded and ordinary version now have the same signature --- sql/item.cc | 28 ++++++++++- sql/item.h | 24 ++++------ sql/mysql_priv.h | 2 +- sql/sql_parse.cc | 2 +- sql/sql_prepare.cc | 115 ++++++++++++++++++++++++++++++--------------- 5 files changed, 114 insertions(+), 57 deletions(-) diff --git a/sql/item.cc b/sql/item.cc index 5aaeffff5d2..bd3b89b396b 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -517,7 +517,33 @@ String *Item_null::val_str(String *str) { null_value=1; return 0;} -/* Item_param related */ +/*********************** Item_param related ******************************/ + +/* + Default function of Item_param::set_param_func, so in case + of malformed packet the server won't SIGSEGV +*/ + +static void +default_set_param_func(Item_param *param, + uchar **pos __attribute__((unused)), + ulong len __attribute__((unused))) +{ + param->set_null(); +} + +Item_param::Item_param(unsigned position) : + value_is_set(FALSE), + item_result_type(STRING_RESULT), + item_type(STRING_ITEM), + item_is_time(FALSE), + long_data_supplied(FALSE), + pos_in_query(position), + set_param_func(default_set_param_func) +{ + name= (char*) "?"; +} + void Item_param::set_null() { DBUG_ENTER("Item_param::set_null"); diff --git a/sql/item.h b/sql/item.h index 727132916f0..832fc775016 100644 --- a/sql/item.h +++ b/sql/item.h @@ -348,16 +348,7 @@ public: bool long_data_supplied; uint pos_in_query; - Item_param(uint position) - { - name= (char*) "?"; - pos_in_query= position; - item_type= STRING_ITEM; - item_result_type = STRING_RESULT; - item_is_time= false; - long_data_supplied= false; - value_is_set= 0; - } + Item_param(uint position); enum Type type() const { return item_type; } double val(); longlong val_int(); @@ -374,11 +365,14 @@ public: void set_time(TIME *tm, timestamp_type type); bool get_time(TIME *tm); void reset() {} -#ifndef EMBEDDED_LIBRARY - void (*set_param_func)(Item_param *param, uchar **pos); -#else - void (*set_param_func)(Item_param *param, uchar **pos, ulong data_len); -#endif + /* + Assign placeholder value from bind data. + Note, that 'len' has different semantics in embedded library (as we + don't need to check that packet is not broken there). See + sql_prepare.cc for details. + */ + void (*set_param_func)(Item_param *param, uchar **pos, ulong len); + enum Item_result result_type () const { return item_result_type; } String *query_val_str(String *str); diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 9fd7e6f5b93..8696a09962c 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -621,7 +621,7 @@ int mysqld_help (THD *thd, const char *text); /* sql_prepare.cc */ void mysql_stmt_prepare(THD *thd, char *packet, uint packet_length); -void mysql_stmt_execute(THD *thd, char *packet); +void mysql_stmt_execute(THD *thd, char *packet, uint packet_length); void mysql_stmt_free(THD *thd, char *packet); void mysql_stmt_reset(THD *thd, char *packet); void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length); diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 97bae472757..3100737aaa5 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -1405,7 +1405,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd, } case COM_EXECUTE: { - mysql_stmt_execute(thd, packet); + mysql_stmt_execute(thd, packet, packet_length); break; } case COM_LONG_DATA: diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index ac5b7847647..ab136668cfb 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -94,7 +94,8 @@ public: bool long_data_used; bool log_full_query; #ifndef EMBEDDED_LIBRARY - bool (*set_params)(Prepared_statement *st, uchar *pos, uchar *read_pos); + bool (*set_params)(Prepared_statement *st, uchar *data, uchar *data_end, + uchar *read_pos); #else bool (*set_params_data)(Prepared_statement *st); #endif @@ -117,14 +118,6 @@ inline bool is_param_null(const uchar *pos, ulong param_no) enum { STMT_QUERY_LOG_LENGTH= 8192 }; -#ifdef EMBEDDED_LIBRARY -#define SET_PARAM_FUNCTION(fn_name) \ -static void fn_name(Item_param *param, uchar **pos, ulong data_len) -#else -#define SET_PARAM_FUNCTION(fn_name) \ -static void fn_name(Item_param *param, uchar **pos) -#endif - enum enum_send_error { DONT_SEND_ERROR= 0, SEND_ERROR }; /* @@ -186,29 +179,38 @@ static bool send_prep_stmt(Prepared_statement *stmt, */ #ifndef EMBEDDED_LIBRARY -static ulong get_param_length(uchar **packet) +static ulong get_param_length(uchar **packet, ulong len) { reg1 uchar *pos= *packet; + if (len < 1) + return 0; if (*pos < 251) { (*packet)++; return (ulong) *pos; } + if (len < 3) + return 0; if (*pos == 252) { (*packet)+=3; return (ulong) uint2korr(pos+1); } + if (len < 4) + return 0; if (*pos == 253) { (*packet)+=4; return (ulong) uint3korr(pos+1); } + if (len < 5) + return 0; (*packet)+=9; // Must be 254 when here + /* TODO: why uint4korr here? (should be uint8korr) */ return (ulong) uint4korr(pos+1); } #else -#define get_param_length(A) data_len +#define get_param_length(packet, len) len #endif /*!EMBEDDED_LIBRARY*/ /* @@ -230,55 +232,80 @@ static ulong get_param_length(uchar **packet) none */ -SET_PARAM_FUNCTION(set_param_tiny) +void set_param_tiny(Item_param *param, uchar **pos, ulong len) { +#ifndef EMBEDDED_LIBRARY + if (len < 1) + return; +#endif param->set_int((longlong)(**pos)); *pos+= 1; } -SET_PARAM_FUNCTION(set_param_short) +void set_param_short(Item_param *param, uchar **pos, ulong len) { +#ifndef EMBEDDED_LIBRARY + if (len < 2) + return; +#endif param->set_int((longlong)sint2korr(*pos)); *pos+= 2; } -SET_PARAM_FUNCTION(set_param_int32) +void set_param_int32(Item_param *param, uchar **pos, ulong len) { +#ifndef EMBEDDED_LIBRARY + if (len < 4) + return; +#endif param->set_int((longlong)sint4korr(*pos)); *pos+= 4; } -SET_PARAM_FUNCTION(set_param_int64) +void set_param_int64(Item_param *param, uchar **pos, ulong len) { +#ifndef EMBEDDED_LIBRARY + if (len < 8) + return; +#endif param->set_int((longlong)sint8korr(*pos)); *pos+= 8; } -SET_PARAM_FUNCTION(set_param_float) +void set_param_float(Item_param *param, uchar **pos, ulong len) { +#ifndef EMBEDDED_LIBRARY + if (len < 4) + return; +#endif float data; float4get(data,*pos); param->set_double((double) data); *pos+= 4; } -SET_PARAM_FUNCTION(set_param_double) +void set_param_double(Item_param *param, uchar **pos, ulong len) { +#ifndef EMBEDDED_LIBRARY + if (len < 8) + return; +#endif double data; float8get(data,*pos); param->set_double((double) data); *pos+= 8; } -SET_PARAM_FUNCTION(set_param_time) +void set_param_time(Item_param *param, uchar **pos, ulong len) { ulong length; - if ((length= get_param_length(pos))) + if ((length= get_param_length(pos, len)) >= 8) { uchar *to= *pos; TIME tm; + /* TODO: why length is compared with 8 here? */ tm.second_part= (length > 8 ) ? (ulong) sint4korr(to+7): 0; tm.day= (ulong) sint4korr(to+1); @@ -294,11 +321,11 @@ SET_PARAM_FUNCTION(set_param_time) *pos+= length; } -SET_PARAM_FUNCTION(set_param_datetime) +void set_param_datetime(Item_param *param, uchar **pos, ulong len) { uint length; - if ((length= get_param_length(pos))) + if ((length= get_param_length(pos, len)) >= 4) { uchar *to= *pos; TIME tm; @@ -324,11 +351,11 @@ SET_PARAM_FUNCTION(set_param_datetime) *pos+= length; } -SET_PARAM_FUNCTION(set_param_date) +void set_param_date(Item_param *param, uchar **pos, ulong len) { ulong length; - if ((length= get_param_length(pos))) + if ((length= get_param_length(pos, len)) >= 4) { uchar *to= *pos; TIME tm; @@ -346,11 +373,11 @@ SET_PARAM_FUNCTION(set_param_date) *pos+= length; } -SET_PARAM_FUNCTION(set_param_str) +void set_param_str(Item_param *param, uchar **pos, ulong len) { - ulong len= get_param_length(pos); - param->set_value((const char *)*pos, len); - *pos+= len; + ulong length= get_param_length(pos, len); + param->set_value((const char *)*pos, length); + *pos+= length; } static void setup_one_conversion_function(Item_param *param, uchar param_type) @@ -405,8 +432,8 @@ static void setup_one_conversion_function(Item_param *param, uchar param_type) and if binary/update log is set, generate the valid query. */ -static bool insert_params_withlog(Prepared_statement *stmt, uchar *pos, - uchar *read_pos) +static bool insert_params_withlog(Prepared_statement *stmt, uchar *null_array, + uchar *read_pos, uchar *data_end) { THD *thd= stmt->thd; Item_param **begin= stmt->param_array; @@ -428,7 +455,7 @@ static bool insert_params_withlog(Prepared_statement *stmt, uchar *pos, res= param->query_val_str(&str); else { - if (is_param_null(pos, it - begin)) + if (is_param_null(null_array, it - begin)) { param->maybe_null= param->null_value= 1; res= &my_null_string; @@ -436,7 +463,9 @@ static bool insert_params_withlog(Prepared_statement *stmt, uchar *pos, else { param->maybe_null= param->null_value= 0; - param->set_param_func(param, &read_pos); + if (read_pos >= data_end) + DBUG_RETURN(1); + param->set_param_func(param, &read_pos, data_end - read_pos); res= param->query_val_str(&str); } } @@ -452,8 +481,8 @@ static bool insert_params_withlog(Prepared_statement *stmt, uchar *pos, } -static bool insert_params(Prepared_statement *stmt, uchar *pos, - uchar *read_pos) +static bool insert_params(Prepared_statement *stmt, uchar *null_array, + uchar *read_pos, uchar *data_end) { Item_param **begin= stmt->param_array; Item_param **end= begin + stmt->param_count; @@ -465,20 +494,23 @@ static bool insert_params(Prepared_statement *stmt, uchar *pos, Item_param *param= *it; if (!param->long_data_supplied) { - if (is_param_null(pos, it - begin)) + if (is_param_null(null_array, it - begin)) param->maybe_null= param->null_value= 1; else { param->maybe_null= param->null_value= 0; - param->set_param_func(param, &read_pos); + if (read_pos >= data_end) + DBUG_RETURN(1); + param->set_param_func(param, &read_pos, data_end - read_pos); } } } DBUG_RETURN(0); } + static bool setup_conversion_functions(Prepared_statement *stmt, - uchar **data) + uchar **data, uchar *data_end) { /* skip null bits */ uchar *read_pos= *data + (stmt->param_count+7) / 8; @@ -495,6 +527,8 @@ static bool setup_conversion_functions(Prepared_statement *stmt, Item_param **end= it + stmt->param_count; for (; it < end; ++it) { + if (read_pos >= data_end) + DBUG_RETURN(1); setup_one_conversion_function(*it, *read_pos); read_pos+= 2; } @@ -1072,7 +1106,7 @@ static void reset_stmt_for_execute(Prepared_statement *stmt) */ -void mysql_stmt_execute(THD *thd, char *packet) +void mysql_stmt_execute(THD *thd, char *packet, uint packet_length) { ulong stmt_id= uint4korr(packet); Prepared_statement *stmt; @@ -1097,10 +1131,11 @@ void mysql_stmt_execute(THD *thd, char *packet) #ifndef EMBEDDED_LIBRARY if (stmt->param_count) { + uchar *packet_end= (uchar *) packet + packet_length - 1; packet+= 4; uchar *null_array= (uchar *) packet; - if (setup_conversion_functions(stmt, (uchar **) &packet) || - stmt->set_params(stmt, null_array, (uchar *) packet)) + if (setup_conversion_functions(stmt, (uchar **) &packet, packet_end) || + stmt->set_params(stmt, null_array, (uchar *) packet, packet_end)) goto set_params_data_err; } #else @@ -1159,6 +1194,7 @@ set_params_data_err: void mysql_stmt_reset(THD *thd, char *packet) { + /* There is always space for 4 bytes in buffer */ ulong stmt_id= uint4korr(packet); Prepared_statement *stmt; @@ -1189,6 +1225,7 @@ void mysql_stmt_reset(THD *thd, char *packet) void mysql_stmt_free(THD *thd, char *packet) { + /* There is always space for 4 bytes in packet buffer */ ulong stmt_id= uint4korr(packet); Prepared_statement *stmt; From 2e0d5eace576b2470c4bdc2649227a47994c4129 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Mar 2004 22:39:36 +0300 Subject: [PATCH 05/12] Fix of compilation failure of latest 4.1 tree: new constants in mysqld.cc exceed unsigned long limit. include/config-win.h: implementation of ULL macro for Windows include/my_global.h: ULL macro defined: we need this macro because ULL qualifier is not defined on systems withoug unsigned long long sql/item_func.cc: new ULL macro caused conflict with class for user level lock ULL. ULL renamed to User_level_lock sql/item_func.h: ULL -> User_level_lock sql/mysql_priv.h: merge error fixed: LL defined in my_global.h sql/mysqld.cc: ULL macro used for long long constants to fix compilation failure on gcc 3.* sql/sql_class.h: ULL renamed to User_level_lock --- include/config-win.h | 1 + include/my_global.h | 8 ++++++++ sql/item_func.cc | 43 +++++++++++++++++++++++-------------------- sql/item_func.h | 4 ++-- sql/mysql_priv.h | 8 -------- sql/mysqld.cc | 8 ++++---- sql/sql_class.h | 2 +- 7 files changed, 39 insertions(+), 35 deletions(-) diff --git a/include/config-win.h b/include/config-win.h index abc1aa7a6ee..518f445861a 100644 --- a/include/config-win.h +++ b/include/config-win.h @@ -95,6 +95,7 @@ functions */ #define LONGLONG_MAX ((__int64) 0x7FFFFFFFFFFFFFFF) #define ULONGLONG_MAX ((unsigned __int64) 0xFFFFFFFFFFFFFFFF) #define LL(A) ((__int64) A) +#define ULL(A) ((unsigned __int64) A) /* Type information */ diff --git a/include/my_global.h b/include/my_global.h index 2a82173979d..4a56741ddbc 100644 --- a/include/my_global.h +++ b/include/my_global.h @@ -842,6 +842,14 @@ typedef char bool; /* Ordinary boolean values 0 1 */ #endif #endif +#ifndef ULL +#ifdef HAVE_LONG_LONG +#define ULL(A) A ## ULL +#else +#define ULL(A) A ## UL +#endif +#endif + /* Defines to make it possible to prioritize register assignments. No longer that important with modern compilers. diff --git a/sql/item_func.cc b/sql/item_func.cc index 0327204dbfd..99a429e48fb 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -1728,7 +1728,7 @@ bool udf_handler::get_arguments() { return 0; } pthread_mutex_t LOCK_user_locks; static HASH hash_user_locks; -class ULL +class User_level_lock { char *key; uint key_length; @@ -1740,7 +1740,7 @@ public: pthread_t thread; ulong thread_id; - ULL(const char *key_arg,uint length, ulong id) + User_level_lock(const char *key_arg,uint length, ulong id) :key_length(length),count(1),locked(1), thread_id(id) { key=(char*) my_memdup((byte*) key_arg,length,MYF(0)); @@ -1754,7 +1754,7 @@ public: } } } - ~ULL() + ~User_level_lock() { if (key) { @@ -1764,11 +1764,12 @@ public: pthread_cond_destroy(&cond); } inline bool initialized() { return key != 0; } - friend void item_user_lock_release(ULL *ull); - friend char *ull_get_key(const ULL *ull,uint *length,my_bool not_used); + friend void item_user_lock_release(User_level_lock *ull); + friend char *ull_get_key(const User_level_lock *ull, uint *length, + my_bool not_used); }; -char *ull_get_key(const ULL *ull,uint *length, +char *ull_get_key(const User_level_lock *ull, uint *length, my_bool not_used __attribute__((unused))) { *length=(uint) ull->key_length; @@ -1796,7 +1797,7 @@ void item_user_lock_free(void) } } -void item_user_lock_release(ULL *ull) +void item_user_lock_release(User_level_lock *ull) { ull->locked=0; if (mysql_bin_log.is_open()) @@ -1852,7 +1853,7 @@ longlong Item_master_pos_wait::val_int() void debug_sync_point(const char* lock_name, uint lock_timeout) { THD* thd=current_thd; - ULL* ull; + User_level_lock* ull; struct timespec abstime; int lock_name_len,error=0; lock_name_len=strlen(lock_name); @@ -1870,7 +1871,7 @@ void debug_sync_point(const char* lock_name, uint lock_timeout) this case, we will not be waiting, but rather, just waste CPU and memory on the whole deal */ - if (!(ull= ((ULL*) hash_search(&hash_user_locks,lock_name, + if (!(ull= ((User_level_lock*) hash_search(&hash_user_locks, lock_name, lock_name_len)))) { pthread_mutex_unlock(&LOCK_user_locks); @@ -1931,7 +1932,7 @@ longlong Item_func_get_lock::val_int() longlong timeout=args[1]->val_int(); struct timespec abstime; THD *thd=current_thd; - ULL *ull; + User_level_lock *ull; int error=0; pthread_mutex_lock(&LOCK_user_locks); @@ -1950,10 +1951,11 @@ longlong Item_func_get_lock::val_int() thd->ull=0; } - if (!(ull= ((ULL*) hash_search(&hash_user_locks,(byte*) res->ptr(), - res->length())))) + if (!(ull= ((User_level_lock *) hash_search(&hash_user_locks, + (byte*) res->ptr(), + res->length())))) { - ull=new ULL(res->ptr(),res->length(), thd->thread_id); + ull=new User_level_lock(res->ptr(),res->length(), thd->thread_id); if (!ull || !ull->initialized()) { delete ull; @@ -2022,7 +2024,7 @@ longlong Item_func_get_lock::val_int() longlong Item_func_release_lock::val_int() { String *res=args[0]->val_str(&value); - ULL *ull; + User_level_lock *ull; longlong result; if (!res || !res->length()) { @@ -2033,8 +2035,9 @@ longlong Item_func_release_lock::val_int() result=0; pthread_mutex_lock(&LOCK_user_locks); - if (!(ull= ((ULL*) hash_search(&hash_user_locks,(const byte*) res->ptr(), - res->length())))) + if (!(ull= ((User_level_lock*) hash_search(&hash_user_locks, + (const byte*) res->ptr(), + res->length())))) { null_value=1; } @@ -3041,7 +3044,7 @@ longlong Item_func_is_free_lock::val_int() { String *res=args[0]->val_str(&value); THD *thd=current_thd; - ULL *ull; + User_level_lock *ull; null_value=0; if (!res || !res->length()) @@ -3051,7 +3054,7 @@ longlong Item_func_is_free_lock::val_int() } pthread_mutex_lock(&LOCK_user_locks); - ull= (ULL*) hash_search(&hash_user_locks,(byte*) res->ptr(), + ull= (User_level_lock *) hash_search(&hash_user_locks, (byte*) res->ptr(), res->length()); pthread_mutex_unlock(&LOCK_user_locks); if (!ull || !ull->locked) @@ -3063,14 +3066,14 @@ longlong Item_func_is_used_lock::val_int() { String *res=args[0]->val_str(&value); THD *thd=current_thd; - ULL *ull; + User_level_lock *ull; null_value=1; if (!res || !res->length()) return 0; pthread_mutex_lock(&LOCK_user_locks); - ull= (ULL*) hash_search(&hash_user_locks,(byte*) res->ptr(), + ull= (User_level_lock *) hash_search(&hash_user_locks, (byte*) res->ptr(), res->length()); pthread_mutex_unlock(&LOCK_user_locks); if (!ull || !ull->locked) diff --git a/sql/item_func.h b/sql/item_func.h index 3890e7c6de5..8fbe5a124da 100644 --- a/sql/item_func.h +++ b/sql/item_func.h @@ -864,9 +864,9 @@ public: ** User level locks */ -class ULL; +class User_level_lock; void item_user_lock_init(void); -void item_user_lock_release(ULL *ull); +void item_user_lock_release(User_level_lock *ull); void item_user_lock_free(void); class Item_func_get_lock :public Item_int_func diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 8696a09962c..8f86d9990fe 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -64,14 +64,6 @@ char* query_table_status(THD *thd,const char *db,const char *table_name); #define PREV_BITS(type,A) ((type) (((type) 1 << (A)) -1)) #define all_bits_set(A,B) ((A) & (B) != (B)) -#ifndef LL -#ifdef HAVE_LONG_LONG -#define LL(A) A ## LL -#else -#define LL(A) A ## L -#endif -#endif - extern CHARSET_INFO *system_charset_info, *files_charset_info ; extern CHARSET_INFO *national_charset_info, *table_alias_charset; diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 9a469e529cf..786390efd6b 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -308,10 +308,10 @@ double log_10[32]; /* 10 potences */ ulonglong log_10_int[20]= { 1, 10, 100, 1000, 10000UL, 100000UL, 1000000UL, 10000000UL, - 100000000UL, 1000000000UL, 10000000000UL, 100000000000UL, - 1000000000000UL, 10000000000000UL, 100000000000000UL, - 1000000000000000UL, 10000000000000000UL, 100000000000000000UL, - 1000000000000000000UL, 10000000000000000000UL + ULL(100000000), ULL(1000000000), ULL(10000000000), ULL(100000000000), + ULL(1000000000000), ULL(10000000000000), ULL(100000000000000), + ULL(1000000000000000), ULL(10000000000000000), ULL(100000000000000000), + ULL(1000000000000000000), ULL(10000000000000000000) }; time_t start_time; diff --git a/sql/sql_class.h b/sql/sql_class.h index 567135b1378..33684f93d93 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -657,7 +657,7 @@ public: points to a lock object if the lock is present. See item_func.cc and chapter 'Miscellaneous functions', for functions GET_LOCK, RELEASE_LOCK. */ - ULL *ull; + User_level_lock *ull; #ifndef DBUG_OFF uint dbug_sentry; // watch out for memory corruption #endif From 5caff162f969b7fcbdf4461ff065e7049ffac72c Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Mar 2004 22:02:47 +0100 Subject: [PATCH 06/12] cleanup --- sql/sql_select.cc | 3 +-- sql/sql_table.cc | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 7605016dbe1..467c1295517 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -842,8 +842,7 @@ JOIN::optimize() for (uint i_h = const_tables; i_h < tables; i_h++) { TABLE* table_h = join_tab[i_h].table; - if (table_h->db_type == DB_TYPE_INNODB) - table_h->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); + table_h->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); } } #endif diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 9d1855ef936..02dc5ba013e 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -2979,12 +2979,10 @@ int mysql_checksum_table(THD *thd, TABLE_LIST *tables, HA_CHECK_OPT *check_opt) /* calculating table's checksum */ ha_checksum crc= 0; - if (t->db_type == DB_TYPE_INNODB) { - /* InnoDB must be told explicitly to retrieve all columns, because - this function does not set field->query_id in the columns to the - current query id */ - t->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); - } + /* InnoDB must be told explicitly to retrieve all columns, because + this function does not set field->query_id in the columns to the + current query id */ + t->file->extra(HA_EXTRA_RETRIEVE_ALL_COLS); if (t->file->rnd_init(1)) protocol->store_null(); From ffad9aeff0fd70204a259c7a87c02c259e4c78e1 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Mar 2004 01:50:35 +0400 Subject: [PATCH 07/12] Yet another fix for spatial parts sql/spatial.cc: should be one step further --- sql/spatial.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/spatial.cc b/sql/spatial.cc index f98799e26d1..ec6bfb307b9 100644 --- a/sql/spatial.cc +++ b/sql/spatial.cc @@ -25,7 +25,7 @@ Geometry::Class_info *Geometry::ci_collection[Geometry::wkb_end]= }; static Geometry::Class_info **ci_collection_end= - Geometry::ci_collection+Geometry::wkb_end; + Geometry::ci_collection+Geometry::wkb_end + 1; Geometry::Class_info::Class_info(const char *name, int type_id, void(*create_func)(void *)): From 4c0d915652466b8f6c7f1bb5b397f59cb5cea8d3 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Mar 2004 02:04:03 +0400 Subject: [PATCH 08/12] Small fix sql/item_strfunc.cc: that's the right fix --- sql/item_strfunc.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 3c8689e1588..2833e1ca016 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -2721,7 +2721,7 @@ String *Item_func_uuid::val_str(String *str) { ulong tmp=sql_rnd_with_mutex(); uchar mac[6]; - unsigned int i; + int i; if (my_gethwaddr(mac)) { /* @@ -2731,7 +2731,7 @@ String *Item_func_uuid::val_str(String *str) randominit() here */ randominit(&uuid_rand, tmp + (ulong)current_thd, tmp + query_id); - for (i=0; i < sizeof(mac); i++) + for (i=0; i < (int)sizeof(mac); i++) mac[i]=(uchar)(my_rnd(&uuid_rand)*255); } s=clock_seq_and_node_str+sizeof(clock_seq_and_node_str)-1; From 321422c86e29199d1d2fb0337640fa819550baa0 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Mar 2004 01:04:04 +0300 Subject: [PATCH 09/12] Intermediate commit of client library (cleanups + fixes of 3 items from flaws list) TODO: * verify that no sequence of API calls produces SIGSEGV. That is, verify that mysql_stmt_init -> mysql_stmt_fetch is OK, or mysql_stmt_prepare -> mysql_stmt_fetch_column is OK and sets meaningful error. * remove alloc_stmt_fields call * revise stmt->state codes and statement states. * there are other items in prepared statements 'to fix' document. Done: - cleanups and comments - revision of prepared statement error codes. - mysql_stmt_prepare is now can always be called (that is, you can reprepare a statement) - new implementation of mysql_stmt_close and fetch cancellation include/errmsg.h: - CR_NOT_ALL_PARAMS_BOUND - this error code wasn't used until now. Apparently it was added in advance, but then interface of mysql_stmt_bind_param changed. Now it's not possible to bind only some parameters - either all or none of parameters are bound. This error code is renamed to CR_PARAMS_NOT_BOUND - CR_FETCH_CANCELLED - error code set on server side when fetch from MYSQL_RES or MYSQL_STMT (in blocking mode) was cancelled because of intercepting call to mysql_stmt_close - CR_NO_DATA - this is proposed error code to return from mysql_stmt_fetch_column if no row was fetched (by any type of fetch). We always can fall back to CR_COMMANDS_OUT_OF_SYNC though. Need reviewer's opinion on this one. include/mysql.h: - added unbuffered_fetch_owner member to MYSQL to point to MYSQL_RES or MYSQL_STMT which is used to fetch result at the moment. This is to be able to set CR_FETCH_CANCELLED error without fantoms. - added unbuffered_fetch_cancelled boolean variable to MYSQL_STMT and MYSQL_RES structures - rename PREP_STMT_STATE -> enum enum_mysql_stmt_state - members of MYSQL_STMT ordered by size. - removed members of MYSQL_STMT: current_row, result_buffered, last_fetched_column, last_fetched_buffer, query - renamed members of MYSQL_STMT: param_buffers -> bind_param_done, res_buffers -> bind_result_done - now mysql_stmt_fetch calls stmt->read_row_func to read row either from buffer or from network. include/sql_common.h: declaration for flush_use_result libmysql/client_settings.h: stmt_close declaration removed libmysql/errmsg.c: Error messages for changed and added error codes. libmysql/libmysql.c: Many changes: - some unused variables removed - cleanups - better error reporting - some function calls commented - alloc_stmt_fields is now called right after execute, to not read mysql->fields of some other statement - new implementation of mysql_stmt_fetch - this is also with cursor fetch in mind (to implement cursor fetch I'll just need to write special read_row function for it, so this change will be local) - implementation of fetch cancellation, including complete rewrite of mysql_stmt_close - now mysql_stmt_free_result doesn't free results of other statements. sql-common/client.c: - implementation of flush_use_result - implementation of fetch cancellation - changed behaviour of mysql_close in regard to mysql_stmt_close - now mysql_close just set stmt->mysql to 0 --- include/errmsg.h | 4 +- include/mysql.h | 52 ++-- include/sql_common.h | 1 + libmysql/client_settings.h | 1 - libmysql/errmsg.c | 16 +- libmysql/libmysql.c | 593 +++++++++++++++++++++++++------------ sql-common/client.c | 98 +++--- 7 files changed, 517 insertions(+), 248 deletions(-) diff --git a/include/errmsg.h b/include/errmsg.h index 8f3ddfa9796..16f220a7ee2 100644 --- a/include/errmsg.h +++ b/include/errmsg.h @@ -67,7 +67,7 @@ extern const char *client_errors[]; /* Error messages */ /* new 4.1 error codes */ #define CR_NULL_POINTER 2028 #define CR_NO_PREPARE_STMT 2029 -#define CR_NOT_ALL_PARAMS_BOUND 2030 +#define CR_PARAMS_NOT_BOUND 2030 #define CR_DATA_TRUNCATED 2031 #define CR_NO_PARAMETERS_EXISTS 2032 #define CR_INVALID_PARAMETER_NO 2033 @@ -87,3 +87,5 @@ extern const char *client_errors[]; /* Error messages */ #define CR_CONN_UNKNOW_PROTOCOL 2046 #define CR_INVALID_CONN_HANDLE 2047 #define CR_SECURE_AUTH 2048 +#define CR_FETCH_CANCELLED 2049 +#define CR_NO_DATA 2050 diff --git a/include/mysql.h b/include/mysql.h index 2b4153bb140..1bcc2a58534 100644 --- a/include/mysql.h +++ b/include/mysql.h @@ -256,6 +256,11 @@ typedef struct st_mysql LIST *stmts; /* list of all statements */ const struct st_mysql_methods *methods; void *thd; + /* + Points to boolean flag in MYSQL_RES or MYSQL_STMT. We set this flag + from mysql_stmt_close if close had to cancel result set of this object. + */ + my_bool *unbuffered_fetch_owner; } MYSQL; typedef struct st_mysql_res { @@ -270,6 +275,8 @@ typedef struct st_mysql_res { MYSQL_ROW row; /* If unbuffered read */ MYSQL_ROW current_row; /* buffer to current row */ my_bool eof; /* Used by mysql_fetch_row */ + /* mysql_stmt_close() had to cancel this result */ + my_bool unbuffered_fetch_cancelled; const struct st_mysql_methods *methods; } MYSQL_RES; @@ -479,7 +486,11 @@ my_bool STDCALL mysql_read_query_result(MYSQL *mysql); */ /* statement state */ -enum PREP_STMT_STATE { MY_ST_UNKNOWN, MY_ST_PREPARE, MY_ST_EXECUTE }; +enum enum_mysql_stmt_state +{ + MYSQL_STMT_INIT_DONE= 1, MYSQL_STMT_PREPARE_DONE, MYSQL_STMT_EXECUTE_DONE, + MYSQL_STMT_FETCH_DONE +}; /* client TIME structure to handle TIME, DATE and TIMESTAMP directly in @@ -525,31 +536,34 @@ typedef struct st_mysql_bind /* statement handler */ typedef struct st_mysql_stmt { - MYSQL *mysql; /* connection handle */ - MYSQL_BIND *params; /* input parameters */ - MYSQL_RES *result; /* resultset */ - MYSQL_BIND *bind; /* row binding */ - MYSQL_FIELD *fields; /* prepare meta info */ + MEM_ROOT mem_root; /* root allocations */ LIST list; /* list to keep track of all stmts */ - unsigned char *current_row; /* unbuffered row */ - unsigned char *last_fetched_buffer; /* last fetched column buffer */ - char *query; /* query buffer */ - MEM_ROOT mem_root; /* root allocations */ - my_ulonglong last_fetched_column; /* last fetched column */ + MYSQL *mysql; /* connection handle */ + MYSQL_BIND *params; /* input parameters */ + MYSQL_BIND *bind; /* output parameters */ + MYSQL_FIELD *fields; /* result set metadata */ + MYSQL_RES *result; /* cached result set */ /* copy of mysql->affected_rows after statement execution */ my_ulonglong affected_rows; + /* + mysql_stmt_fetch() calls this function to fetch one row (it's different + for buffered, unbuffered and cursor fetch). + */ + int (*read_row_func)(struct st_mysql_stmt *stmt, + unsigned char **row); unsigned long stmt_id; /* Id for prepared statement */ unsigned int last_errno; /* error code */ - unsigned int param_count; /* parameters count */ - unsigned int field_count; /* fields count */ - enum PREP_STMT_STATE state; /* statement state */ + unsigned int param_count; /* inpute parameters count */ + unsigned int field_count; /* number of columns in result set */ + enum enum_mysql_stmt_state state; /* statement state */ char last_error[MYSQL_ERRMSG_SIZE]; /* error message */ char sqlstate[SQLSTATE_LENGTH+1]; - my_bool long_alloced; /* flag to indicate long alloced */ - my_bool send_types_to_server; /* Types sent to server */ - my_bool param_buffers; /* param bound buffers */ - my_bool res_buffers; /* output bound buffers */ - my_bool result_buffered; /* Results buffered */ + /* Types of input parameters should be sent to server */ + my_bool send_types_to_server; + my_bool bind_param_done; /* input buffers were supplied */ + my_bool bind_result_done; /* output buffers were supplied */ + /* mysql_stmt_close() had to cancel this result */ + my_bool unbuffered_fetch_cancelled; } MYSQL_STMT; diff --git a/include/sql_common.h b/include/sql_common.h index cde53786f83..3f50008a922 100644 --- a/include/sql_common.h +++ b/include/sql_common.h @@ -25,6 +25,7 @@ extern "C" { MYSQL_FIELD *unpack_fields(MYSQL_DATA *data,MEM_ROOT *alloc,uint fields, my_bool default_value, uint server_capabilities); void free_rows(MYSQL_DATA *cur); +void flush_use_result(MYSQL *mysql); my_bool mysql_autenticate(MYSQL *mysql, const char *passwd); void free_old_query(MYSQL *mysql); void end_server(MYSQL *mysql); diff --git a/libmysql/client_settings.h b/libmysql/client_settings.h index e4475d76958..9fc8e67df27 100644 --- a/libmysql/client_settings.h +++ b/libmysql/client_settings.h @@ -22,7 +22,6 @@ extern my_string mysql_unix_port; CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION) sig_handler pipe_sig_handler(int sig __attribute__((unused))); -my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_free); void read_user_name(char *name); my_bool send_file_to_server(MYSQL *mysql, const char *filename); diff --git a/libmysql/errmsg.c b/libmysql/errmsg.c index 569267ddb37..e651c13897f 100644 --- a/libmysql/errmsg.c +++ b/libmysql/errmsg.c @@ -54,7 +54,7 @@ const char *client_errors[]= "Malformed packet", "Invalid use of null pointer", "Statement not prepared", - "Not all parameters data supplied", + "Parameters data was not supplied", "Data truncated", "No parameters exists in the statement", "Invalid parameter number", @@ -72,7 +72,9 @@ const char *client_errors[]= "Can't open shared memory. Can't send the request event to server (%lu)", "Wrong or unknown protocol", "Invalid connection handle", - "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)" + "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)", + "Row retrieval was cancelled by mysql_stmt_close() call", + "Attempt to read column without prior row fetch" }; /* Start of code added by Roberto M. Serqueira - martinsc@uol.com.br - 05.24.2001 */ @@ -110,7 +112,7 @@ const char *client_errors[]= "Malformed packet", "Invalid use of null pointer", "Statement not prepared", - "Not all parameters data supplied", + "Parameters data was not supplied", "Data truncated", "No parameters exists in the statement", "Invalid parameter number", @@ -128,7 +130,9 @@ const char *client_errors[]= "Can't open shared memory. Can't send the request event to server (%lu)", "Wrong or unknown protocol", "Invalid connection handle", - "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)" + "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)", + "Row retrieval was cancelled by mysql_stmt_close() call", + "Attempt to read column without prior row fetch" }; #else /* ENGLISH */ @@ -182,7 +186,9 @@ const char *client_errors[]= "Can't open shared memory. Can't send the request event to server (%lu)", "Wrong or unknown protocol", "Invalid connection handle", - "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)" + "Connection using old (pre 4.1.1) authentication protocol refused (client option 'secure_auth' enabled)", + "Row retrieval was cancelled by mysql_stmt_close() call", + "Attempt to read column without prior row fetch" }; #endif diff --git a/libmysql/libmysql.c b/libmysql/libmysql.c index 0f274021f20..91927e44e49 100644 --- a/libmysql/libmysql.c +++ b/libmysql/libmysql.c @@ -638,6 +638,7 @@ int cli_read_change_user_result(MYSQL *mysql, char *buff, const char *passwd) return 0; } + my_bool STDCALL mysql_change_user(MYSQL *mysql, const char *user, const char *passwd, const char *db) { @@ -866,6 +867,7 @@ STDCALL mysql_set_master(MYSQL* mysql, const char* host, return 0; } + int STDCALL mysql_add_slave(MYSQL* mysql, const char* host, unsigned int port, @@ -987,6 +989,7 @@ mysql_list_tables(MYSQL *mysql, const char *wild) DBUG_RETURN (mysql_store_result(mysql)); } + MYSQL_FIELD *cli_list_fields(MYSQL *mysql) { MYSQL_DATA *query; @@ -1062,6 +1065,7 @@ mysql_list_processes(MYSQL *mysql) DBUG_RETURN(mysql_store_result(mysql)); } + #ifdef USE_OLD_FUNCTIONS int STDCALL mysql_create_db(MYSQL *mysql, const char *db) @@ -1099,6 +1103,7 @@ mysql_refresh(MYSQL *mysql,uint options) DBUG_RETURN(simple_command(mysql,COM_REFRESH,(char*) bits,1,0)); } + int STDCALL mysql_kill(MYSQL *mysql,ulong pid) { @@ -1126,6 +1131,7 @@ mysql_dump_debug_info(MYSQL *mysql) DBUG_RETURN(simple_command(mysql,COM_DEBUG,0,0,0)); } + const char *cli_read_statistics(MYSQL *mysql) { mysql->net.read_pos[mysql->packet_length]=0; /* End of stat string */ @@ -1139,6 +1145,7 @@ const char *cli_read_statistics(MYSQL *mysql) return (char*) mysql->net.read_pos; } + const char * STDCALL mysql_stat(MYSQL *mysql) { @@ -1576,6 +1583,10 @@ static my_bool my_realloc_str(NET *net, ulong length) Prepare related implementations ********************************************************************/ +static int stmt_read_row_unbuffered(MYSQL_STMT *stmt, unsigned char **row); +static int stmt_read_row_buffered(MYSQL_STMT *stmt, unsigned char **row); +static int stmt_read_row_no_data(MYSQL_STMT *stmt, unsigned char **row); + /* Read the prepared statement results .. @@ -1591,13 +1602,12 @@ static my_bool my_realloc_str(NET *net, ulong length) my_bool cli_read_prepare_result(MYSQL *mysql, MYSQL_STMT *stmt) { uchar *pos; - uint field_count; - ulong length, param_count; + uint field_count, param_count; MYSQL_DATA *fields_data; DBUG_ENTER("read_prepare_result"); mysql= mysql->last_used_con; - if ((length= net_safe_read(mysql)) == packet_error) + if (net_safe_read(mysql) == packet_error) DBUG_RETURN(1); pos= (uchar*) mysql->net.read_pos; @@ -1634,7 +1644,7 @@ MYSQL_STMT * STDCALL mysql_prepare(MYSQL *mysql, const char *query, stmt= mysql_stmt_init(mysql); if (stmt && mysql_stmt_prepare(stmt, query, query_length)) { - stmt_close(stmt, 0); + mysql_stmt_close(stmt); DBUG_RETURN(0); } DBUG_RETURN(stmt); @@ -1646,6 +1656,7 @@ MYSQL_STMT * STDCALL mysql_prepare(MYSQL *mysql, const char *query, SYNOPSIS mysql_stmt_init() mysql connection handle + RETURN VALUE statement structure upon success and NULL if out of memory @@ -1664,20 +1675,35 @@ mysql_stmt_init(MYSQL *mysql) DBUG_RETURN(0); } - init_alloc_root(&stmt->mem_root,8192,0); + init_alloc_root(&stmt->mem_root, 2048, 2048); mysql->stmts= list_add(mysql->stmts, &stmt->list); stmt->list.data= stmt; - stmt->state= MY_ST_UNKNOWN; + stmt->state= MYSQL_STMT_INIT_DONE; stmt->mysql= mysql; + stmt->read_row_func= stmt_read_row_no_data; + /* The rest of statement members was bzeroed inside malloc */ DBUG_RETURN(stmt); } /* - Prepare server side statement with query. + Prepare server side statement with query: + SYNOPSIS + mysql_stmt_prepare() + query statement to prepare + length statement length - Also update the total parameter count along with resultset - metadata information by reading from server + DESCRIPTION + - if this is a re-prepare of the statement, first close previous data + structure on the server and free old statement data + - send the query to server and get back number of placeholders, + number of columns in result set (if any), and result set metadata. + At the same time allocate memory for input and output parameters + to have less checks in mysql_stmt_bind_{param, result}. + + RETURN VALUES + 0 success + !0 error */ @@ -1687,17 +1713,44 @@ mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, ulong length) MYSQL *mysql= stmt->mysql; DBUG_ENTER("mysql_stmt_prepare"); - DBUG_ASSERT(mysql != 0); - - /* In case we reprepare this handle with another statement */ - free_root(&stmt->mem_root, MYF(0)); - - /* stmt->query is never used yet */ - if (!(stmt->query= strmake_root(&stmt->mem_root, query, length))) + if (!mysql) { - set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate); + /* mysql can be reset in mysql_close called from mysql_reconnect */ + set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate); DBUG_RETURN(1); } + + if ((int) stmt->state > (int) MYSQL_STMT_INIT_DONE) + { + /* This is second prepare with another statement */ + char buff[4]; + + mysql_stmt_free_result(stmt); + /* + These members must be reset for API to + function in case of error or misuse. + */ + stmt->bind_param_done= stmt->bind_result_done= FALSE; + stmt->param_count= stmt->field_count= 0; + stmt->last_errno= 0; + stmt->last_error[0]= '\0'; + free_root(&stmt->mem_root, MYF(MY_KEEP_PREALLOC)); + + int4store(buff, stmt->stmt_id); + /* + If there was a 'use' result from another statement, or from + mysql_use_result it won't be freed in mysql_stmt_free_result and + we should get 'Commands out of sync' here. + */ + if (simple_command(mysql, COM_CLOSE_STMT, buff, 4, 1)) + { + set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, + mysql->net.sqlstate); + DBUG_RETURN(1); + } + stmt->state= MYSQL_STMT_INIT_DONE; + } + if (simple_command(mysql, COM_PREPARE, query, length, 1)) { set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, @@ -1706,8 +1759,18 @@ mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, ulong length) } if ((*mysql->methods->read_prepare_result)(mysql, stmt)) + { + set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, + mysql->net.sqlstate); DBUG_RETURN(1); + } + /* + alloc_root will return valid address even in case param_count + and field_count are zero. Thus we should never rely on stmt->bind + or stmt->params when checking for existence of placeholders or + result set. + */ if (!(stmt->params= (MYSQL_BIND *) alloc_root(&stmt->mem_root, sizeof(MYSQL_BIND)* (stmt->param_count + @@ -1717,26 +1780,22 @@ mysql_stmt_prepare(MYSQL_STMT *stmt, const char *query, ulong length) DBUG_RETURN(1); } stmt->bind= stmt->params + stmt->param_count; - stmt->state= MY_ST_PREPARE; - mysql->status= MYSQL_STATUS_READY; + stmt->state= MYSQL_STMT_PREPARE_DONE; DBUG_PRINT("info", ("Parameter count: %ld", stmt->param_count)); DBUG_RETURN(0); } /* Get the execute query meta information for non-select - statements (on demand). + statements. */ -unsigned int alloc_stmt_fields(MYSQL_STMT *stmt) +static unsigned int alloc_stmt_fields(MYSQL_STMT *stmt) { MYSQL_FIELD *fields, *field, *end; MEM_ROOT *alloc= &stmt->mem_root; MYSQL *mysql= stmt->mysql->last_used_con; - if (stmt->state != MY_ST_EXECUTE || !mysql->field_count) - return 0; - stmt->field_count= mysql->field_count; /* @@ -1782,20 +1841,25 @@ mysql_stmt_result_metadata(MYSQL_STMT *stmt) MYSQL_RES *result; DBUG_ENTER("mysql_stmt_result_metadata"); - if (!stmt->field_count || !stmt->fields) - { - if (!alloc_stmt_fields(stmt)) - DBUG_RETURN(0); - } - if (!(result=(MYSQL_RES*) my_malloc(sizeof(*result)+ - sizeof(ulong)*stmt->field_count, - MYF(MY_WME | MY_ZEROFILL)))) - return 0; + /* + stmt->fields is only defined if stmt->field_count is not null; + stmt->field_count is initialized in prepare. + */ + if (!stmt->field_count) + DBUG_RETURN(0); - result->methods= stmt->mysql->methods; - result->eof=1; /* Marker for buffered */ + if (!(result=(MYSQL_RES*) my_malloc(sizeof(*result), + MYF(MY_WME | MY_ZEROFILL)))) + { + set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate); + DBUG_RETURN(0); + } + + result->methods= stmt->mysql->methods; + result->eof= 1; /* Marker for buffered */ result->fields= stmt->fields; result->field_count= stmt->field_count; + /* The rest of members of 'result' was bzeroed inside malloc */ DBUG_RETURN(result); } @@ -1989,11 +2053,9 @@ static void store_param_null(NET *net, MYSQL_BIND *param) client application */ - static my_bool store_param(MYSQL_STMT *stmt, MYSQL_BIND *param) { - MYSQL *mysql= stmt->mysql; - NET *net = &mysql->net; + NET *net= &stmt->mysql->net; DBUG_ENTER("store_param"); DBUG_PRINT("enter",("type: %d, buffer:%lx, length: %lu is_null: %d", param->buffer_type, @@ -2059,6 +2121,12 @@ int cli_stmt_execute(MYSQL_STMT *stmt) uint null_count; my_bool result; + if (!stmt->bind_param_done) + { + set_stmt_error(stmt, CR_PARAMS_NOT_BOUND, unknown_sqlstate); + DBUG_RETURN(1); + } + net_clear(net); /* Sets net->write_pos */ /* Reserve place for null-marker bytes */ null_count= (stmt->param_count+7) /8; @@ -2093,7 +2161,6 @@ int cli_stmt_execute(MYSQL_STMT *stmt) set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate); DBUG_RETURN(1); } - net->write_pos= net->buff; /* Reset for net_write() */ result= execute(stmt, param_data, length); stmt->send_types_to_server=0; my_free(param_data, MYF(MY_WME)); @@ -2102,22 +2169,146 @@ int cli_stmt_execute(MYSQL_STMT *stmt) DBUG_RETURN((int) execute(stmt,0,0)); } +/* + Read one row from buffered result set. Result set is created by prior + call to mysql_stmt_store_result(). + SYNOPSIS + stmt_read_row_buffered() + + RETURN VALUE + 0 - success; *row is set to valid row pointer (row data + is stored in result set buffer) + MYSQL_NO_DATA - end of result set. *row is set to NULL +*/ + +static int stmt_read_row_buffered(MYSQL_STMT *stmt, unsigned char **row) +{ + MYSQL_RES *result= stmt->result; + + if (result && result->data_cursor) + { + *row= (uchar *) result->data_cursor->data; + result->data_cursor= result->data_cursor->next; + return 0; + } + *row= 0; + return MYSQL_NO_DATA; +} + +/* + Read one row from network: unbuffered non-cursor fetch. + If last row was read, or error occured, erase this statement + from record pointing to object unbuffered fetch is performed from. + + SYNOPSIS + stmt_read_row_unbuffered() + stmt statement handle + row pointer to write pointer to row data; + + RETURN VALUE + 0 - success; *row contains valid address of a row; + row data is stored in network buffer + 1 - error; error code is written to + stmt->last_{errno,error}; *row is not changed + MYSQL_NO_DATA - end of file was read from network; + *row is to NULL +*/ + +static int stmt_read_row_unbuffered(MYSQL_STMT *stmt, unsigned char **row) +{ + int rc= 1; + MYSQL *mysql= stmt->mysql; + /* + This function won't be called if stmt->field_count is zero + or execution wasn't done: this is ensured by mysql_stmt_execute. + */ + if (!mysql) + { + set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate); + return 1; + } + if (mysql->status != MYSQL_STATUS_GET_RESULT) + { + set_stmt_error(stmt, stmt->unbuffered_fetch_cancelled ? + CR_FETCH_CANCELLED : CR_COMMANDS_OUT_OF_SYNC, + unknown_sqlstate); + goto error; + } + if ((*mysql->methods->unbuffered_fetch)(mysql, (char**) row)) + { + set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, + mysql->net.sqlstate); + goto error; + } + if (!*row) + { + mysql->status= MYSQL_STATUS_READY; + rc= MYSQL_NO_DATA; + goto error; + } + return 0; +error: + if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled) + mysql->unbuffered_fetch_owner= 0; + return rc; +} + +/* + Default read row function to not SIGSEGV in client in + case of wrong sequence of API calls. +*/ + +static int +stmt_read_row_no_data(MYSQL_STMT *stmt __attribute__((unused)), + unsigned char **row __attribute__((unused))) +{ + if ((int) stmt->state < (int) MYSQL_STMT_PREPARE_DONE) + { + set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate); + return 1; + } + return MYSQL_NO_DATA; +} + /* Execute the prepared query */ int STDCALL mysql_stmt_execute(MYSQL_STMT *stmt) { + MYSQL *mysql= stmt->mysql; DBUG_ENTER("mysql_stmt_execute"); - if ((*stmt->mysql->methods->stmt_execute)(stmt)) + if (!mysql) + { + set_stmt_error(stmt, CR_SERVER_LOST, unknown_sqlstate); DBUG_RETURN(1); + } + + mysql_stmt_free_result(stmt); + /* + No need to check for stmt->state: if the statement wasn't + prepared we'll get 'unknown statemenet handler' error from server. + */ + if (mysql->methods->stmt_execute(stmt)) + DBUG_RETURN(1); + if (!stmt->field_count && mysql->field_count) + { + /* + This is 'SHOW'/'EXPLAIN'-like query. Current implementation of + prepared statements can't send result set metadata for this queries + on prepare stage. Read it now. + */ + alloc_stmt_fields(stmt); + } - stmt->state= MY_ST_EXECUTE; - mysql_free_result(stmt->result); - stmt->result= (MYSQL_RES *)0; - stmt->result_buffered= 0; - stmt->current_row= 0; + stmt->state= MYSQL_STMT_EXECUTE_DONE; + if (stmt->field_count) + { + stmt->mysql->unbuffered_fetch_owner= &stmt->unbuffered_fetch_cancelled; + stmt->unbuffered_fetch_cancelled= FALSE; + stmt->read_row_func= stmt_read_row_unbuffered; + } DBUG_RETURN(0); } @@ -2155,6 +2346,16 @@ my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind) MYSQL_BIND *param, *end; DBUG_ENTER("mysql_stmt_bind_param"); + if (!stmt->param_count) + { + if ((int) stmt->state < (int) MYSQL_STMT_PREPARE_DONE) + { + set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate); + DBUG_RETURN(1); + } + DBUG_RETURN(0); + } + /* Allocated on prepare */ memcpy((char*) stmt->params, (char*) bind, sizeof(MYSQL_BIND) * stmt->param_count); @@ -2166,13 +2367,6 @@ my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind) param->param_number= count++; param->long_data_used= 0; - /* - If param->length is not given, change it to point to buffer_length. - This way we can always use *param->length to get the length of data - */ - if (!param->length) - param->length= ¶m->buffer_length; - /* If param->is_null is not set, then the value can never be NULL */ if (!param->is_null) param->is_null= &int_is_null_false; @@ -2239,10 +2433,16 @@ my_bool STDCALL mysql_stmt_bind_param(MYSQL_STMT *stmt, MYSQL_BIND * bind) param->buffer_type, count); DBUG_RETURN(1); } + /* + If param->length is not given, change it to point to buffer_length. + This way we can always use *param->length to get the length of data + */ + if (!param->length) + param->length= ¶m->buffer_length; } /* We have to send/resendtype information to MySQL */ - stmt->send_types_to_server= 1; - stmt->param_buffers= 1; + stmt->send_types_to_server= TRUE; + stmt->bind_param_done= TRUE; DBUG_RETURN(0); } @@ -2276,6 +2476,16 @@ mysql_stmt_send_long_data(MYSQL_STMT *stmt, uint param_number, DBUG_ASSERT(stmt != 0); DBUG_PRINT("enter",("param no : %d, data : %lx, length : %ld", param_number, data, length)); + + /* + We only need to check for stmt->param_count, if it's not null + prepare was done. + */ + 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 || @@ -2846,9 +3056,20 @@ my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind) DBUG_ENTER("mysql_stmt_bind_result"); DBUG_ASSERT(stmt != 0); - if (!(bind_count= stmt->field_count) && - !(bind_count= alloc_stmt_fields(stmt))) + if (!stmt->field_count) + { + if ((int) stmt->state < (int) MYSQL_STMT_PREPARE_DONE) + { + set_stmt_error(stmt, CR_NO_PREPARE_STMT, unknown_sqlstate); + } DBUG_RETURN(0); + } + bind_count= stmt->field_count; + + /* + We only need to check that stmt->field_count - if it is not null + stmt->bind was initialized in mysql_stmt_prepare + */ memcpy((char*) stmt->bind, (char*) bind, sizeof(MYSQL_BIND)*bind_count); @@ -2929,7 +3150,7 @@ my_bool STDCALL mysql_stmt_bind_result(MYSQL_STMT *stmt, MYSQL_BIND *bind) DBUG_RETURN(1); } } - stmt->res_buffers= 1; + stmt->bind_result_done= TRUE; DBUG_RETURN(0); } @@ -2943,9 +3164,18 @@ static int stmt_fetch_row(MYSQL_STMT *stmt, uchar *row) MYSQL_BIND *bind, *end; MYSQL_FIELD *field, *field_end; uchar *null_ptr, bit; + /* + Precondition: if stmt->field_count is zero or row is NULL, read_row_* + function must return no data. + */ + DBUG_ASSERT(stmt->field_count); + DBUG_ASSERT(row); - if (!row || !stmt->res_buffers) + if (!stmt->bind_result_done) + { + /* If output parameters were not bound we should just return success */ return 0; + } null_ptr= row; row+= (stmt->field_count+9)/8; /* skip null bits */ @@ -2994,57 +3224,22 @@ int cli_unbuffered_fetch(MYSQL *mysql, char **row) int STDCALL mysql_stmt_fetch(MYSQL_STMT *stmt) { - MYSQL *mysql= stmt->mysql; + int rc; uchar *row; DBUG_ENTER("mysql_stmt_fetch"); - stmt->last_fetched_column= 0; /* reset */ - if (stmt->result_buffered) /* buffered */ + if ((rc= (*stmt->read_row_func)(stmt, &row)) || + (rc= stmt_fetch_row(stmt, row))) { - MYSQL_RES *res; - - if (!(res= stmt->result)) - goto no_data; - - if (!res->data_cursor) - { - stmt->current_row= 0; - goto no_data; - } - row= (uchar *)res->data_cursor->data; - res->data_cursor= res->data_cursor->next; + stmt->state= MYSQL_STMT_PREPARE_DONE; /* XXX: this is buggy */ + stmt->read_row_func= stmt_read_row_no_data; } - else /* un-buffered */ + else { - 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, - mysql->net.sqlstate); - DBUG_RETURN(1); - } - if (!row) - { - mysql->status= MYSQL_STATUS_READY; - stmt->current_row= 0; - goto no_data; - } + /* This is to know in mysql_stmt_fetch_column that data was fetched */ + stmt->state= MYSQL_STMT_FETCH_DONE; } - - stmt->current_row= row; - DBUG_RETURN(stmt_fetch_row(stmt, row)); - -no_data: - DBUG_PRINT("info", ("end of data")); - DBUG_RETURN(MYSQL_NO_DATA); /* no more data */ + DBUG_RETURN(rc); } @@ -3065,13 +3260,22 @@ no_data: */ int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, - uint column, ulong offset) + uint column, ulong offset) { MYSQL_BIND *param= stmt->bind+column; DBUG_ENTER("mysql_stmt_fetch_column"); - if (!stmt->current_row) - goto no_data; + if ((int) stmt->state < (int) MYSQL_STMT_FETCH_DONE) + { + set_stmt_error(stmt, CR_NO_DATA, unknown_sqlstate); + return 1; + } + if (column >= stmt->field_count) + { + set_stmt_error(stmt, CR_INVALID_PARAMETER_NO, unknown_sqlstate); + DBUG_RETURN(1); + } + if (param->null_field) { @@ -3092,10 +3296,6 @@ int STDCALL mysql_stmt_fetch_column(MYSQL_STMT *stmt, MYSQL_BIND *bind, fetch_results(bind, field, &row); } DBUG_RETURN(0); - -no_data: - DBUG_PRINT("info", ("end of data")); - DBUG_RETURN(MYSQL_NO_DATA); /* no more data */ } @@ -3111,7 +3311,7 @@ MYSQL_DATA *cli_read_binary_rows(MYSQL_STMT *stmt) MYSQL_DATA *result; MYSQL_ROWS *cur, **prev_ptr; NET *net = &mysql->net; - DBUG_ENTER("read_binary_rows"); + DBUG_ENTER("cli_read_binary_rows"); mysql= mysql->last_used_con; if ((pkt_len= net_safe_read(mysql)) == packet_error) @@ -3181,33 +3381,37 @@ int STDCALL mysql_stmt_store_result(MYSQL_STMT *stmt) if (!stmt->field_count) DBUG_RETURN(0); - if (mysql->status != MYSQL_STATUS_GET_RESULT) + if ((int) stmt->state < (int) MYSQL_STMT_EXECUTE_DONE || + mysql->status != MYSQL_STATUS_GET_RESULT) { set_stmt_error(stmt, CR_COMMANDS_OUT_OF_SYNC, unknown_sqlstate); DBUG_RETURN(1); } - mysql->status= MYSQL_STATUS_READY; /* server is ready */ - if (!(result= (MYSQL_RES*) my_malloc((uint) (sizeof(MYSQL_RES)+ - sizeof(ulong) * - stmt->field_count), + if (!(result= (MYSQL_RES*) my_malloc(sizeof(MYSQL_RES), MYF(MY_WME | MY_ZEROFILL)))) { set_stmt_error(stmt, CR_OUT_OF_MEMORY, unknown_sqlstate); DBUG_RETURN(1); } result->methods= mysql->methods; - stmt->result_buffered= 1; - if (!(result->data= (*stmt->mysql->methods->read_binary_rows)(stmt))) + if ((result->data= (*mysql->methods->read_binary_rows)(stmt))) + { + result->row_count= result->data->rows; + result->data_cursor= result->data->data; + } + else if (stmt->last_errno) { my_free((gptr) result,MYF(0)); - DBUG_RETURN(0); + DBUG_RETURN(1); } - mysql->affected_rows= result->row_count= result->data->rows; - stmt->affected_rows= result->row_count; - result->data_cursor= result->data->data; + mysql->affected_rows= stmt->affected_rows= result->row_count; result->fields= stmt->fields; result->field_count= stmt->field_count; + /* The rest of MYSQL_RES members were bzeroed inside my_malloc */ stmt->result= result; + stmt->read_row_func= stmt_read_row_buffered; + mysql->unbuffered_fetch_owner= 0; /* set in stmt_execute */ + mysql->status= MYSQL_STATUS_READY; /* server is ready */ DBUG_RETURN(0); /* Data buffered, must be fetched with mysql_stmt_fetch() */ } @@ -3292,6 +3496,40 @@ my_ulonglong STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt) DBUG_RETURN(0); } +my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt) +{ + DBUG_ENTER("mysql_stmt_free_result"); + + DBUG_ASSERT(stmt != 0); + + if ((int) stmt->state > (int) MYSQL_STMT_INIT_DONE) + { + MYSQL *mysql= stmt->mysql; + + if (stmt->result) + { + /* Result buffered */ + mysql_free_result(stmt->result); + stmt->result= 0; + } + else if (mysql && stmt->field_count + && (int) stmt->state > (int) MYSQL_STMT_PREPARE_DONE) + { + if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled) + mysql->unbuffered_fetch_owner= 0; + if (mysql->status != MYSQL_STATUS_READY) + { + /* There is a result set and it belongs to this statement */ + flush_use_result(mysql); + mysql->status= MYSQL_STATUS_READY; + } + } + stmt->state= MYSQL_STMT_PREPARE_DONE; + stmt->read_row_func= stmt_read_row_no_data; + } + DBUG_RETURN(0); +} + /******************************************************************** statement error handling and close *********************************************************************/ @@ -3300,82 +3538,55 @@ my_ulonglong STDCALL mysql_stmt_num_rows(MYSQL_STMT *stmt) Close the statement handle by freeing all alloced resources SYNOPSIS - mysql_stmt_free_result() + mysql_stmt_close() stmt Statement handle - skip_list Flag to indicate delete from list or not + RETURN VALUES 0 ok 1 error */ -my_bool STDCALL mysql_stmt_free_result(MYSQL_STMT *stmt) -{ - MYSQL *mysql; - DBUG_ENTER("mysql_stmt_free_result"); - - DBUG_ASSERT(stmt != 0); - - mysql= stmt->mysql; - if (mysql->status != MYSQL_STATUS_READY) - { - /* Clear the current execution status */ - DBUG_PRINT("warning",("Not all packets read, clearing them")); - for (;;) - { - ulong pkt_len; - if ((pkt_len= net_safe_read(mysql)) == packet_error) - break; - if (pkt_len <= 8 && mysql->net.read_pos[0] == 254) - break; - } - mysql->status= MYSQL_STATUS_READY; - } - mysql_free_result(stmt->result); - stmt->result= 0; - stmt->result_buffered= 0; - stmt->current_row= 0; - DBUG_RETURN(0); -} - - -my_bool stmt_close(MYSQL_STMT *stmt, my_bool skip_free) -{ - MYSQL *mysql; - DBUG_ENTER("stmt_close"); - - DBUG_ASSERT(stmt != 0); - - if (!(mysql= stmt->mysql)) - { - if (!skip_free) - my_free((gptr) stmt, MYF(MY_WME)); - DBUG_RETURN(0); - } - mysql_stmt_free_result(stmt); - if (stmt->state == MY_ST_PREPARE || stmt->state == MY_ST_EXECUTE) - { - char buff[4]; - int4store(buff, stmt->stmt_id); - if (skip_free || simple_command(mysql, COM_CLOSE_STMT, buff, 4, 1)) - { - set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, - mysql->net.sqlstate); - stmt->mysql= NULL; /* connection isn't valid anymore */ - DBUG_RETURN(1); - } - } - stmt->field_count= 0; - free_root(&stmt->mem_root, MYF(0)); - mysql->stmts= list_delete(mysql->stmts, &stmt->list); - mysql->status= MYSQL_STATUS_READY; - my_free((gptr) stmt, MYF(MY_WME)); - DBUG_RETURN(0); -} - - my_bool STDCALL mysql_stmt_close(MYSQL_STMT *stmt) { - return stmt_close(stmt, 0); + MYSQL *mysql= stmt->mysql; + int rc= 0; + DBUG_ENTER("mysql_stmt_close"); + + mysql_free_result(stmt->result); /* if result is buffered */ + free_root(&stmt->mem_root, MYF(0)); + + if (mysql) + { + mysql->stmts= list_delete(mysql->stmts, &stmt->list); + if ((int) stmt->state > (int) MYSQL_STMT_INIT_DONE) + { + char buff[4]; + + if (mysql->unbuffered_fetch_owner == &stmt->unbuffered_fetch_cancelled) + mysql->unbuffered_fetch_owner= 0; + if (mysql->status != MYSQL_STATUS_READY) + { + /* + Flush result set of the connection. If it does not belong + to this statement, set a warning. + */ + flush_use_result(mysql); + if (mysql->unbuffered_fetch_owner) + *mysql->unbuffered_fetch_owner= TRUE; + mysql->status= MYSQL_STATUS_READY; + } + int4store(buff, stmt->stmt_id); + if ((rc= simple_command(mysql, COM_CLOSE_STMT, buff, 4, 1))) + { + set_stmt_errmsg(stmt, mysql->net.last_error, mysql->net.last_errno, + mysql->net.sqlstate); + } + } + } + + my_free((gptr) stmt, MYF(MY_WME)); + + DBUG_RETURN(test(rc)); } /* @@ -3388,6 +3599,10 @@ my_bool STDCALL mysql_stmt_reset(MYSQL_STMT *stmt) MYSQL *mysql; DBUG_ENTER("mysql_stmt_reset"); DBUG_ASSERT(stmt != 0); + + /* If statement hasnt been prepared there is nothing to reset */ + if ((int) stmt->state < (int) MYSQL_STMT_PREPARE_DONE) + DBUG_RETURN(0); mysql= stmt->mysql->last_used_con; int4store(buff, stmt->stmt_id); /* Send stmt id to server */ diff --git a/sql-common/client.c b/sql-common/client.c index 5d2df4a0ddf..6d0da338543 100644 --- a/sql-common/client.c +++ b/sql-common/client.c @@ -712,6 +712,34 @@ void set_mysql_error(MYSQL *mysql, int errcode, const char *sqlstate) net->last_errno= errcode; strmov(net->last_error, ER(errcode)); strmov(net->sqlstate, sqlstate); + + DBUG_VOID_RETURN; +} + +/* + Flush result set sent from server +*/ + +void flush_use_result(MYSQL *mysql) +{ + /* Clear the current execution status */ + DBUG_PRINT("warning",("Not all packets read, clearing them")); + for (;;) + { + ulong pkt_len; + if ((pkt_len=net_safe_read(mysql)) == packet_error) + break; + if (pkt_len <= 8 && mysql->net.read_pos[0] == 254) + { + if (protocol_41(mysql)) + { + char *pos= (char*) mysql->net.read_pos; + mysql->warning_count=uint2korr(pos); pos+=2; + mysql->server_status=uint2korr(pos); pos+=2; + } + break; /* End of data */ + } + } } @@ -752,26 +780,16 @@ mysql_free_result(MYSQL_RES *result) DBUG_PRINT("enter",("mysql_res: %lx",result)); if (result) { - if (result->handle && result->handle->status == MYSQL_STATUS_USE_RESULT) + MYSQL *mysql= result->handle; + if (mysql) { - DBUG_PRINT("warning",("Not all rows in set where read; Ignoring rows")); - for (;;) + if (mysql->unbuffered_fetch_owner == &result->unbuffered_fetch_cancelled) + mysql->unbuffered_fetch_owner= 0; + if (mysql->status == MYSQL_STATUS_USE_RESULT) { - ulong pkt_len; - if ((pkt_len=net_safe_read(result->handle)) == packet_error) - break; - if (pkt_len <= 8 && result->handle->net.read_pos[0] == 254) - { - if (protocol_41(result->handle)) - { - char *pos= (char*) result->handle->net.read_pos; - result->handle->warning_count=uint2korr(pos); pos+=2; - result->handle->server_status=uint2korr(pos); pos+=2; - } - break; /* End of data */ - } + flush_use_result(mysql); + mysql->status=MYSQL_STATUS_READY; } - result->handle->status=MYSQL_STATUS_READY; } free_rows(result->data); if (result->fields) @@ -2177,12 +2195,13 @@ void STDCALL mysql_close(MYSQL *mysql) #ifdef MYSQL_CLIENT if (mysql->stmts) { - /* Free any open prepared statements */ - LIST *element, *next_element; - for (element= mysql->stmts; element; element= next_element) + /* Reset connection handle in all prepared statements. */ + LIST *element; + for (element= mysql->stmts; element; element= element->next) { - next_element= element->next; - stmt_close((MYSQL_STMT *)element->data, 1); + MYSQL_STMT *stmt= (MYSQL_STMT *) element->data; + stmt->mysql= 0; + /* No need to call list_delete for statement here */ } mysql->stmts= 0; } @@ -2372,9 +2391,10 @@ MYSQL_RES * STDCALL mysql_store_result(MYSQL *mysql) result->fields= mysql->fields; result->field_alloc= mysql->field_alloc; result->field_count= mysql->field_count; - result->current_field=0; - result->current_row=0; /* Must do a fetch first */ + /* The rest of result members is bzeroed in malloc */ mysql->fields=0; /* fields is now in result */ + /* just in case this was mistakenly called after mysql_stmt_execute() */ + mysql->unbuffered_fetch_owner= 0; DBUG_RETURN(result); /* Data fetched */ } @@ -2423,6 +2443,7 @@ static MYSQL_RES * cli_use_result(MYSQL *mysql) result->current_row= 0; mysql->fields=0; /* fields is now in result */ mysql->status=MYSQL_STATUS_USE_RESULT; + mysql->unbuffered_fetch_owner= &result->unbuffered_fetch_cancelled; DBUG_RETURN(result); /* Data is read to be fetched */ } @@ -2439,19 +2460,30 @@ mysql_fetch_row(MYSQL_RES *res) { /* Unbufferred fetch */ if (!res->eof) { - if (!(read_one_row(res->handle,res->field_count,res->row, res->lengths))) + MYSQL *mysql= res->handle; + if (mysql->status != MYSQL_STATUS_USE_RESULT) + { + set_mysql_error(mysql, + res->unbuffered_fetch_cancelled ? + CR_FETCH_CANCELLED : CR_COMMANDS_OUT_OF_SYNC, + unknown_sqlstate); + } + else if (!(read_one_row(mysql, res->field_count, res->row, res->lengths))) { res->row_count++; DBUG_RETURN(res->current_row=res->row); } - else - { - DBUG_PRINT("info",("end of data")); - res->eof=1; - res->handle->status=MYSQL_STATUS_READY; - /* Don't clear handle in mysql_free_results */ - res->handle=0; - } + DBUG_PRINT("info",("end of data")); + res->eof=1; + mysql->status=MYSQL_STATUS_READY; + /* + Reset only if owner points to us: there is a chance that somebody + started new query after mysql_stmt_close(): + */ + if (mysql->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled) + mysql->unbuffered_fetch_owner= 0; + /* Don't clear handle in mysql_free_result */ + res->handle=0; } DBUG_RETURN((MYSQL_ROW) NULL); } From 33d7754e4dcd48fde8444a7bbb2094cc53f27529 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Mar 2004 00:04:06 +0200 Subject: [PATCH 10/12] Moved errno to strtod.c as one can use m_string.h without errno.h --- include/m_string.h | 3 --- strings/strtod.c | 10 +++++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/m_string.h b/include/m_string.h index 73c8a385a70..21aa736acd4 100644 --- a/include/m_string.h +++ b/include/m_string.h @@ -218,9 +218,6 @@ extern int is_prefix(const char *, const char *); /* Conversion routines */ double my_strtod(const char *str, char **end); double my_atof(const char *nptr); -#ifndef EOVERFLOW -#define EOVERFLOW 84 -#endif #ifdef USE_MY_ITOA extern char *my_itoa(int val,char *dst,int radix); diff --git a/strings/strtod.c b/strings/strtod.c index 92a4700cfc4..aa0f7aa4336 100644 --- a/strings/strtod.c +++ b/strings/strtod.c @@ -26,9 +26,13 @@ */ -#include "my_base.h" +#include "my_base.h" /* Includes errno.h */ #include "m_ctype.h" +#ifndef EOVERFLOW +#define EOVERFLOW 84 +#endif + static double scaler10[] = { 1.0, 1e10, 1e20, 1e30, 1e40, 1e50, 1e60, 1e70, 1e80, 1e90 }; @@ -37,10 +41,6 @@ static double scaler1[] = { }; -#ifndef HUGE_VAL /* Should be +Infinitive */ -#define HUGE_VAL DBL_MAX -#endif - double my_strtod(const char *str, char **end) { double result= 0.0; From 0952bf2da5a8adf26794b730f98bfa18f95d28db Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Mar 2004 00:42:10 +0200 Subject: [PATCH 11/12] Fixed memory overrun in spatial init code sql/spatial.cc: Fixed memory overrun sql/spatial.h: Fixed memory overrun --- sql/spatial.cc | 2 +- sql/spatial.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/spatial.cc b/sql/spatial.cc index ec6bfb307b9..ab415d9af10 100644 --- a/sql/spatial.cc +++ b/sql/spatial.cc @@ -19,7 +19,7 @@ /***************************** Gis_class_info *******************************/ -Geometry::Class_info *Geometry::ci_collection[Geometry::wkb_end]= +Geometry::Class_info *Geometry::ci_collection[Geometry::wkb_end+1]= { NULL, NULL, NULL, NULL, NULL, NULL, NULL }; diff --git a/sql/spatial.h b/sql/spatial.h index 26396dd0f90..213bbbf6323 100644 --- a/sql/spatial.h +++ b/sql/spatial.h @@ -251,7 +251,7 @@ public: } bool envelope(String *result) const; - static Geometry::Class_info *ci_collection[Geometry::wkb_end]; + static Geometry::Class_info *ci_collection[Geometry::wkb_end+1]; protected: static Class_info *find_class(int type_id) From 17f8d3d725f4b465a8a0dc8e96b04cd973c47fd5 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 16 Mar 2004 02:41:36 +0200 Subject: [PATCH 12/12] Portability fix --- sql/spatial.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sql/spatial.h b/sql/spatial.h index 213bbbf6323..cf07b364bb3 100644 --- a/sql/spatial.h +++ b/sql/spatial.h @@ -211,8 +211,7 @@ public: virtual int geometry_n(uint32 num, String *result) const { return -1; } public: - static Geometry *Geometry::create_by_typeid(Geometry_buffer *buffer, - int type_id) + static Geometry *create_by_typeid(Geometry_buffer *buffer, int type_id) { Class_info *ci; if (!(ci= find_class((int) type_id))) @@ -251,7 +250,7 @@ public: } bool envelope(String *result) const; - static Geometry::Class_info *ci_collection[Geometry::wkb_end+1]; + static Class_info *ci_collection[wkb_end+1]; protected: static Class_info *find_class(int type_id)