Bug #33205 Hexadecimal / Bit-Field Literals Are Parsed Incorrectly
Submitted: 13 Dec 2007 9:42 Modified: 13 Dec 2007 12:49
Reporter: Asuka Kenji Siu Ching Pong (Basic Quality Contributor) (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Parser Severity:S1 (Critical)
Version:5.0.50-enterprise-gpl-nt, 5.0.45-log OS:Any
Assigned to: CPU Architecture:Any
Tags: Bit-Field, Hexadecimal, literal, parser, qc

[13 Dec 2007 9:42] Asuka Kenji Siu Ching Pong
Description:
According to the documentation found at "http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-values.html", hexadecimal literals in the form 0xABCD are interpreted as strings by default, and numbers in numeric contexts.

According to the documentation found at "http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_char", the CHAR() function interprets each argument N as an integer.

Therefore, in "CHAR(0x41)", "0x41" should be interpreted as an integer. However, it seems that the database mis-handled it.

The problem is found to be more serious that almost all statements related to hexadecimal literals in the form 0xABCD are affected (See How To Repeat below).

"explain extended select 0x41 as ColA" produces strange result "select ColA AS `ColA`" in the explain message. Later statements also produce "???" in the explain message.

This bug may be related to bug #33198.

How to repeat:
mysql> explain extended select 65;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+-------------------+
| Level | Code | Message           |
+-------+------+-------------------+
| Note  | 1003 | select 65 AS `65` |
+-------+------+-------------------+
1 row in set (0.00 sec)

mysql> explain extended select 65 as ColA;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+---------------------+
| Level | Code | Message             |
+-------+------+---------------------+
| Note  | 1003 | select 65 AS `ColA` |
+-------+------+---------------------+
1 row in set (0.00 sec)

mysql> explain extended select 0x41;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+-----------------------+
| Level | Code | Message               |
+-------+------+-----------------------+
| Note  | 1003 | select 0x41 AS `0x41` |
+-------+------+-----------------------+
1 row in set (0.00 sec)

mysql> explain extended select 0x41 as ColA;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+-----------------------+
| Level | Code | Message               |
+-------+------+-----------------------+
| Note  | 1003 | select ColA AS `ColA` |
+-------+------+-----------------------+
1 row in set (0.00 sec)

mysql> explain extended select 0 + 0x41;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+--------------------------------+
| Level | Code | Message                        |
+-------+------+--------------------------------+
| Note  | 1003 | select (0 + ???) AS `0 + 0x41` |
+-------+------+--------------------------------+
1 row in set (0.00 sec)

mysql> explain extended select 0x41 + 0;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+--------------------------------+
| Level | Code | Message                        |
+-------+------+--------------------------------+
| Note  | 1003 | select (??? + 0) AS `0x41 + 0` |
+-------+------+--------------------------------+
1 row in set (0.00 sec)

mysql> explain extended select cast(0x41 as unsigned);
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+----------------------------------------------------------+
| Level | Code | Message                                                  |
+-------+------+----------------------------------------------------------+
| Note  | 1003 | select cast(??? as unsigned) AS `cast(0x41 as unsigned)` |
+-------+------+----------------------------------------------------------+
1 row in set (0.00 sec)

mysql> explain extended select cast(0x41 as char);
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+---------------------------------------------------------------+
| Level | Code | Message                                                       |
+-------+------+---------------------------------------------------------------+
| Note  | 1003 | select cast(??? as char charset utf8) AS `cast(0x41 as char)` |
+-------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> explain extended select char(0x41);
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No tables used |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> show warnings;
+-------+------+----------------------------------+
| Level | Code | Message                          |
+-------+------+----------------------------------+
| Note  | 1003 | select char(???) AS `char(0x41)` |
+-------+------+----------------------------------+
1 row in set (0.00 sec)
[13 Dec 2007 9:53] Asuka Kenji Siu Ching Pong
It is found that hexadecimal literals in the form x'ABCD' suffer the same problem.
[13 Dec 2007 9:55] Asuka Kenji Siu Ching Pong
Test case in MySQL test case format

Attachment: bug33205.test (application/octet-stream, text), 982 bytes.

[13 Dec 2007 9:57] Asuka Kenji Siu Ching Pong
Result of my run

Attachment: bug33205.result (application/octet-stream, text), 13.16 KiB.

[13 Dec 2007 10:14] Asuka Kenji Siu Ching Pong
It is found that bit-field literals in the form b'1010' and 0b1010 also suffer the same problem.
[13 Dec 2007 11:41] Susanne Ebrecht
Sorry, I can't find the error here using MySQL 5.0.51. Please, try it with the newest versions.

Many thanks for choosing MySQL.
[13 Dec 2007 12:49] Asuka Kenji Siu Ching Pong
Seems fixed in 5.0.52-enterprise-gpl-nt.