Bug #68505 bundled libedit input bug using gbk
Submitted: 27 Feb 2013 6:41 Modified: 1 Mar 2013 6:52
Reporter: jiang xiaobing Email Updates:
Status: Verified Impact on me:
Category:MySQL Server: Command-line Clients Severity:S2 (Serious)
Version:mysql5.5,mysql5.6 OS:Any
Assigned to: CPU Architecture:Any

[27 Feb 2013 6:41] jiang xiaobing
when using mysql bundled libedit, using locale "zh_CN.GBK" can't input chinese.

How to repeat:
1. build wtih bunlded libedit
2. export LC_ALL="zh_CN.GBK"
3. run mysql, and input the chinese encoded using GBK


$ client/mysql -V
client/mysql  Ver 14.14 Distrib 5.5.30, for Linux (x86_64) using  EditLine wrapper

$ ldd client/mysql
        linux-vdso.so.1 (0x00007ffffafff000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00000033c0800000)
        libz.so.1 => /lib64/libz.so.1 (0x0000003890e00000)
        librt.so.1 => /lib64/librt.so.1 (0x00000033c1000000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003890600000)
        libncurses.so.5 => /lib64/libncurses.so.5 (0x0000003c5b400000)
        libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/libstdc++.so.6 (0x0000003141200000)
        libm.so.6 => /lib64/libm.so.6 (0x0000003890a00000)
        libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/libgcc_s.so.1 (0x0000003140e00000)
        libc.so.6 => /lib64/libc.so.6 (0x000000388fe00000)
        /lib64/ld-linux-x86-64.so.2 (0x000000388fa00000)

Suggested fix:
bing@bing ~/source/bzr/mysql-server/mysq-5.5-build $ bzr diff
=== modified file 'cmd-line-utils/libedit/terminal.c'
--- cmd-line-utils/libedit/terminal.c   2011-10-21 12:40:12 +0000
+++ cmd-line-utils/libedit/terminal.c   2013-02-27 06:26:18 +0000
@@ -1251,7 +1251,7 @@
        memset(&state, 0, sizeof(mbstate_t));
        if (c == (Int)MB_FILL_CHAR)
                return 0;
-       i = ct_encode_char(buf, (size_t)MB_CUR_MAX, c, &state);
+       i = ct_encode_char(buf, (size_t)MB_LEN_MAX, c, &state);
        if (i <= 0)
                return (int)i;
        buf[i] = '\0';
[28 Feb 2013 9:46] Sveta Smirnova
Thank you for the report.

I can not repeat described behavior with Chinese text I pasted from some text which Google found for me. Please send repeatable text case and its results:

- Chinese text you are trying to use. Better if this would be SQL file similar to mine:

set names 'gbk';
select '汉语/漢語';

- Copy-paste or screenshot of MySQL session. So we can guess what happens for you after you input this Chinese text.
[28 Feb 2013 13:01] MySQL Verification Team
Verified as described.

When I copy "select '汉语/漢語'" and paste it to mysql CLI, then multi byte characters are truncated like below:

mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)

mysql> select '/h';
| /h |
| /h |
1 row in set (0.00 sec)

I cannot input SJIS characters either.

The problem doesn't happen on ReadLine.

I applied the suggested patch, but the problem still happens.
[1 Mar 2013 6:52] jiang xiaobing
Hi Sveta Smirnova:

yes, you may be using the utf8 encoding chinese. and may using iconv to convert.
and you can't using sql file to test. because mysql will not using the readline function.

and I think "set names" is not neccessary. because this bug is not about mysql server.

to Mikiya Okuno:

I google the SJIS, it seems like GBK. both using two bytes for non ascii. 
this should fixed by this patch.