[MDEV-6572] "USE dbname" with a bad sequence erroneously connects to a wrong database Created: 2014-08-13  Updated: 2020-05-05  Resolved: 2015-03-16

Status: Closed
Project: MariaDB Server
Component/s: Character Sets
Affects Version/s: 5.3.12, 5.5.39, 10.0.14
Fix Version/s: 10.1.4

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Blocks
is blocked by MDEV-6566 Different INSERT behaviour on bad byt... Closed

 Description   

I create a database with some question marks in the name:

DROP DATABASE IF EXISTS `test????`;
CREATE DATABASE `test????`;

Now I run a "USE" query with a bad byte sequence:

SET NAMES utf8;
USE `test��`;
SELECT DATABASE(), HEX(DATABASE());

(notice. '��' is a four-byte UTF-8 character, which is considered as a wrong
byte sequence by MariaDB's 3-byte "utf8").

It erroneously connects to the database 'test????':

ariaDB [(none)]> SET NAMES utf8;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> USE `test��`;
Database changed
MariaDB [test��]> SELECT DATABASE(), HEX(DATABASE());
+------------+------------------+
| DATABASE() | HEX(DATABASE())  |
+------------+------------------+
| test????   | 746573743F3F3F3F |
+------------+------------------+
1 row in set (0.00 sec)

The correct behaviour would be report either "unknown database",
or "bad database name" or "Invalid utf8 character string: 'test\xF0\x9F\x98\x81'".

The latter would be consistent with what's reported on attempt to
create a database with such a broken name:

MariaDB [test]> CREATE DATABASE `test��`;
ERROR 1300 (HY000): Invalid utf8 character string: 'test\xF0\x9F\x98\x81'

A similar problem also happens with table names.
I create a table with question marks in its name:

SET NAMES utf8;
DROP TABLE IF EXISTS `test????`;
CREATE TABLE `test????` (a INT);
INSERT INTO `test????` values (10);

Now I change the connection character set to utf8mb4 and run a SELECT query:

SET NAMES utf8mb4;
SELECT * FROM `test��������`;

It returns rows from the table `test????`:

+------+
| a    |
+------+
|   10 |
+------+
1 row in set (0.01 sec)

The expected behaviour would be to return some error.


Generated at Thu Feb 08 07:12:56 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.