From aa8677bc32fd80ee9ab08717369d23a4ef011717 Mon Sep 17 00:00:00 2001 From: Martin Hansson Date: Tue, 16 Jun 2009 16:36:15 +0200 Subject: [PATCH] Bug#45168: assertion with convert() and empty set value The assertion in String::copy was added in order to avoid valgrind errors when the destination was the same as the source. Eased restriction to allow for the case when str == NULL. mysql-test/r/func_set.result: Bug#45168: Test result mysql-test/t/func_set.test: Bug#45168: Test case sql/item_strfunc.cc: Bug#45168: Code cleanup and grammar correction in comment sql/sql_string.cc: Bug#45168: Fix --- mysql-test/r/func_set.result | 13 +++++++++++++ mysql-test/t/func_set.test | 13 +++++++++++++ sql/item_strfunc.cc | 5 ++--- sql/sql_string.cc | 15 ++++++++++++--- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/mysql-test/r/func_set.result b/mysql-test/r/func_set.result index ecdc35ac4cd..14ebc8203ec 100644 --- a/mysql-test/r/func_set.result +++ b/mysql-test/r/func_set.result @@ -146,3 +146,16 @@ NULL 0 0 drop table t1; +CREATE TABLE t1( a SET('a', 'b', 'c') ); +CREATE TABLE t2( a SET('a', 'b', 'c') ); +INSERT INTO t1 VALUES ('d'); +Warnings: +Warning 1265 Data truncated for column 'a' at row 1 +INSERT INTO t2 VALUES (''); +SELECT CONVERT( a USING latin1 ) FROM t1; +CONVERT( a USING latin1 ) + +SELECT CONVERT( a USING latin1 ) FROM t2; +CONVERT( a USING latin1 ) + +DROP TABLE t1, t2; diff --git a/mysql-test/t/func_set.test b/mysql-test/t/func_set.test index 5f37cd2a13e..294efa8caf1 100644 --- a/mysql-test/t/func_set.test +++ b/mysql-test/t/func_set.test @@ -84,3 +84,16 @@ engine=myisam default charset=latin1; insert into t1 values (''),(null),(null),(''),(''),(''); select find_in_set(f1,f1) as a from t1,(select find_in_set(f1,f1) as b from t1) a; drop table t1; +# +# Bug#45168: assertion with convert() and empty set value +# +CREATE TABLE t1( a SET('a', 'b', 'c') ); +CREATE TABLE t2( a SET('a', 'b', 'c') ); + +INSERT INTO t1 VALUES ('d'); +INSERT INTO t2 VALUES (''); + +SELECT CONVERT( a USING latin1 ) FROM t1; +SELECT CONVERT( a USING latin1 ) FROM t2; + +DROP TABLE t1, t2; diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 2d580e4d9a8..be94f19f597 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -2710,13 +2710,12 @@ String *Item_func_conv_charset::val_str(String *str) return null_value ? 0 : &str_value; /* Here we don't pass 'str' as a parameter to args[0]->val_str() - as 'str' may points to 'str_value' (e.g. see Item::save_in_field()), + as 'str' may point to 'str_value' (e.g. see Item::save_in_field()), which we use below to convert string. Use argument's 'str_value' instead. */ - String *arg= args[0]->val_str(&args[0]->str_value);; + String *arg= args[0]->val_str(&args[0]->str_value); uint dummy_errors; - arg= args[0]->val_str(&args[0]->str_value); if (!arg) { null_value=1; diff --git a/sql/sql_string.cc b/sql/sql_string.cc index 61731f3b984..7759985ba85 100644 --- a/sql/sql_string.cc +++ b/sql/sql_string.cc @@ -322,14 +322,23 @@ bool String::set_or_copy_aligned(const char *str,uint32 arg_length, return copy_aligned(str, arg_length, offset, cs); } - /* Copy with charset conversion */ + +/** + Copies the character data into this String, with optional character set + conversion. + + @return + FALSE ok + TRUE Could not allocate result buffer + +*/ bool String::copy(const char *str, uint32 arg_length, CHARSET_INFO *from_cs, CHARSET_INFO *to_cs, uint *errors) { uint32 offset; - - DBUG_ASSERT(str != Ptr); + + DBUG_ASSERT(!str || str != Ptr); if (!needs_conversion(arg_length, from_cs, to_cs, &offset)) {