Bug #78932 mysqldump --routines misquotes database names
Submitted: 22 Oct 2015 23:56 Modified: 19 Nov 2015 15:33
Reporter: Felipe Gasper (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: mysqldump Command-line Client Severity:S2 (Serious)
Version:5.1 OS:Any
Assigned to: CPU Architecture:Any

[22 Oct 2015 23:56] Felipe Gasper
Description:
When I run mysqldump on a database named “haha\ha” with no other arguments, the output comes out fine.

When I add the --routines argument, though, it apparently misquotes the database name such that the backslash gets doubled. This produces an error since I have no database named “haha\\ha”.

felipe@Macintosh 18:37:23 ~
> sudo /usr/local/mysql/bin/mysqldump --routines --socket=/opt/local/var/run/mysql56/mysqld.sock 'haha\ha'
-- MySQL dump 10.13  Distrib 5.7.9, for osx10.9 (x86_64)
--
-- Host: localhost    Database: haha\ha
-- ------------------------------------------------------
-- Server version	5.6.27

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Dumping routines for database 'haha\ha'
--
mysqldump: Couldn't execute 'use `haha\\ha`': Unknown database 'haha\\ha' (1049)

How to repeat:
1) Make a database named: haha\ha

2) mysqldump 'haha\ha'
OBSERVE: success

3) mysqldump --routines 'haha\ha'
OBSERVE: failure; the error message suggests that it’s trying to double-up the backslash on a USE statement.

Suggested fix:
mysqldump.c has this command:

mysql_real_escape_string_quote(mysql, db_name_buff,
                                 db, (ulong)strlen(db), '\'');

… which looks like it ought to be passing in '`' rather than '\''.

The same problem goes back at least as far as 5.1.73.
[27 Oct 2015 17:16] OCA Admin
Contribution submitted via Github - Bug#78932: mysqldump --routines misquotes database names 
(*) Contribution by Felipe Gasper (Github FGasper, mysql-server/pull/29#issuecomment-151513197): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_48547477.txt (text/plain), 856 bytes.