Bug #12363 character_set_results is nullable, but value_ptr returns string "NULL"
Submitted: 3 Aug 2005 23:41 Modified: 13 Sep 2005 22:45
Reporter: Timothy Smith Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1 OS:Any (any)
Assigned to: Alexander Barkov CPU Architecture:Any

[3 Aug 2005 23:41] Timothy Smith
Description:

See "How to repeat" section.

How to repeat:
I tested this with a fresh BK pull, on FreeBSD 5.0.4.

11:32 ~/m/41/m$ mysql test -e 'select version(); set character_set_results = NULL; select ifnull(@@character_set_results, "it really is null") as foo'
+------------------+
| version()        |
+------------------+
| 4.1.14-debug-log |
+------------------+
+------+
| foo  |
+------+
| NULL |
+------+

It seems that character_set_results is the only nullable system variable.

This caused the customer a problem when processing the mysqldump output with Java; the @OLD_CHARACTER_SET_RESULTS variable gets set to the string "NULL", and then when trying to reset @@CHARACTER_SET_RESULTS, it is of course an error that there is no character set named "NULL".

Suggested fix:

I tried editing sql/set_var.cc:sys_var_character_set::value_ptr() to return NULL instead of "NULL" , but it segfaults:

0x080b3044 in sys_var::item (this=0x848c0d4, thd=0xa1fe018,
    var_type=170040400, base=0x0) at item.h:731
731       {
(gdb) bt full
#0  0x080b3044 in sys_var::item (this=0x848c0d4, thd=0xa1fe018, 
    var_type=170040400, base=0x0) at item.h:731
        str = 0x0
#1  0x08066f2e in Item_func_get_system_var::fix_fields (this=0xa2290b0, 
    thd=0xa1fe018, tables=0x0, ref=0xa2291e4) at item_func.cc:2891
        _db_func_ = 0xbf1c554c "�U\034�y\t\006\b�\220\"\n\030�\037\n"
        _db_file_ = 0xa1fe56c "\030\214M\b\030�!\n\0300\"\n\030�!\n\030�!\n\017"
        item = (class Item *) 0x83086e8
        _db_level_ = 3206305108
        _db_framep_ = (char **) 0x7
#2  0x08060979 in Item_func::fix_fields (this=0xa2291a8, thd=0xa1fe018, 
    tables=0x0, ref=0xa229224) at item_func.cc:155
        item = (class Item *) 0xa2291a8
        arg = (class Item **) 0xa2291e4
        arg_end = (class Item **) 0xa2291ec
        buff = "\000\000\000\000d\233\"\n\034V\034�@�\037\n\000\000\000\000d\233\"\n\034V\034�w�\r\b/\t\000\000�U\034��U\034��U\034�\000\000\000\000�\220\"\n�U\034�\001\000\000"
        __func__ = "fix_fields"
#3  0x080dd446 in setup_fields (thd=0xa1fe018, ref_pointer_array=0xa229c00, 
    tables=0x0, fields=@0xa229c00, sum_func_list=0xa229b64) at sql_base.cc:2438
        _db_func_ = 0x9ca <Error reading address 0x9ca: Bad address>
        _db_file_ = 0xbf1c560c ",V\034�@�\037\n@\222\"\nd\233\"\n�V\034�\r%\016\b\030�\037\n"
        ref = (class Item **) 0xa229c00
        item = (class Item *) 0xa2291a8
        it = {<base_list_iterator> = {list = 0xa1fe1a8, el = 0xa229220, 
    prev = 0xa1fe1a8, current = 0xa229220}, <No data fields>}
        _db_level_ = 3206305296
        _db_framep_ = (char **) 0xbf1c5614
        set_query_id = Error accessing memory address 0x0: Bad address.
[4 Aug 2005 0:33] Timothy Smith
This patch makes it work - but I bet it's not quite correct.

12:31 ~/m/41/a$ bk diffs -u sql
===== sql/set_var.cc 1.169 vs edited =====
--- 1.169/sql/set_var.cc        2005-07-29 10:37:00 +12:00
+++ edited/sql/set_var.cc       2005-08-04 12:29:21 +12:00
@@ -1603,8 +1603,14 @@
     Item_string *tmp;
     pthread_mutex_lock(&LOCK_global_system_variables);
     char *str= (char*) value_ptr(thd, var_type, base);
-    tmp= new Item_string(str, strlen(str),
-                         system_charset_info, DERIVATION_SYSCONST);
+    if (!str)
+    {
+      tmp= new Item_empty_string("", 0);
+      tmp->null_value= 1;
+    }
+    else
+      tmp= new Item_string(str, strlen(str),
+                            system_charset_info, DERIVATION_SYSCONST);
     pthread_mutex_unlock(&LOCK_global_system_variables);
     return tmp;
   }
@@ -1894,7 +1900,7 @@
                                       LEX_STRING *base)
 {
   CHARSET_INFO *cs= ci_ptr(thd,type)[0];
-  return cs ? (byte*) cs->csname : (byte*) "NULL";
+  return cs ? (byte*) cs->csname : NULL;
 }
[30 Aug 2005 12:15] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/internals/29026
[5 Sep 2005 7:48] Alexander Barkov
Fixed in 4.1.15 and 5.0.13
[13 Sep 2005 22:45] Mike Hillyer
Documented in 5.0.13 and 4.1.15 changelogs:

<listitem>
        <para>
          The value of <literal>character_set_results</literal> could be set to <literal>NULL</literal>,
          but returned the string <literal>"NULL"</literal> when retrieved. (Bug #12363)
        </para>
      </listitem>