Skip to content

Commit f45b87a

Browse files
author
Anushree Prakash B
committed
Bug#29998457 - MYSQLDUMP GENERATES INVALID INSERT
STATEMENTS WITH VARBINARY COLUMNS DESCRIPTION =========== Binary and varbinary columns are now tagged with "_binary " in the mysqldump output. However, the memory allocated for the insert statement does not take the length of the '_binary ' string into account. The resulting pointer arithmetic causes invalid insert statements being generated in the mysqldump output. FIX === Account the memory for the "_binary " string that gets added in front of the string while reallocating the memory. RB: 24098
1 parent 7e589bb commit f45b87a

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

client/mysqldump.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License, version 2.0,
@@ -4043,16 +4043,19 @@ static void dump_table(char *table, char *db)
40434043
if (!(field->flags & NUM_FLAG))
40444044
{
40454045
/*
4046-
"length * 2 + 2" is OK for both HEX and non-HEX modes:
4046+
"length * 2 + 2" is OK for HEX mode:
40474047
- In HEX mode we need exactly 2 bytes per character
40484048
plus 2 bytes for '0x' prefix.
40494049
- In non-HEX mode we need up to 2 bytes per character,
4050-
plus 2 bytes for leading and trailing '\'' characters.
4051-
Also we need to reserve 1 byte for terminating '\0'.
4050+
plus 2 bytes for leading and trailing '\'' characters
4051+
and reserve 1 byte for terminating '\0'.
4052+
In addition to this, for the blob type, we need to
4053+
reserve for the "_binary " string that gets added in
4054+
front of the string in the dump.
40524055
*/
4053-
dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
40544056
if (opt_hex_blob && is_blob)
40554057
{
4058+
dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1);
40564059
dynstr_append_checked(&extended_row, "0x");
40574060
extended_row.length+= mysql_hex_string(extended_row.str +
40584061
extended_row.length,
@@ -4063,6 +4066,8 @@ static void dump_table(char *table, char *db)
40634066
}
40644067
else
40654068
{
4069+
dynstr_realloc_checked(&extended_row,length * 2 + 2 + 1 +
4070+
(is_blob? strlen("_binary ") : 0));
40664071
if (is_blob)
40674072
{
40684073
/*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Bug#29998457 MYSQLDUMP GENERATES INVALID INSERT STATEMENTS WITH VARBINARY COLUMNS
2+
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE = '';
3+
Warnings:
4+
Warning 3090 Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
5+
CREATE DATABASE test_bug29998457;
6+
USE test_bug29998457;
7+
#Pre-test cleanup
8+
DROP TABLE IF EXISTS t1;
9+
CREATE TABLE t1 (
10+
pk INT,
11+
v1 VARCHAR(255),
12+
v2 VARCHAR(255),
13+
v3 VARCHAR(255),
14+
v4 VARCHAR(128),
15+
v5 VARCHAR(64),
16+
v6 VARCHAR(16),
17+
v7 VARCHAR(16),
18+
v8 VARCHAR(8),
19+
v9 VARBINARY(32),
20+
v10 VARBINARY(32));
21+
SELECT '12345678901234567890123456789012345678901234567890' INTO @s;
22+
SELECT CONCAT(@s, @s, @s, @s, @s) INTO @s1;
23+
INSERT INTO t1 VALUES (1, @s1, @s1, @s1, @s1, @s1, @s1, @s1, @s1, '','NULL');
24+
Warnings:
25+
Warning 1265 Data truncated for column 'v4' at row 1
26+
Warning 1265 Data truncated for column 'v5' at row 1
27+
Warning 1265 Data truncated for column 'v6' at row 1
28+
Warning 1265 Data truncated for column 'v7' at row 1
29+
Warning 1265 Data truncated for column 'v8' at row 1
30+
SELECT * FROM test_bug29998457.t1;
31+
pk v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
32+
1 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 1234567890123456789012345678901234567890123456789012345678901234 1234567890123456 1234567890123456 12345678 NULL
33+
Warning (Code 3090): Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
34+
Warning (Code 3090): Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
35+
SELECT * FROM test_bug29998457.t1;
36+
pk v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
37+
1 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678 1234567890123456789012345678901234567890123456789012345678901234 1234567890123456 1234567890123456 12345678 NULL
38+
DROP DATABASE test_bug29998457;
39+
SET SQL_MODE = @OLD_SQL_MODE;
40+
Warnings:
41+
Warning 3090 Changing sql mode 'NO_AUTO_CREATE_USER' is deprecated. It will be removed in a future release.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Embedded server doesn't support external clients
2+
--source include/not_embedded.inc
3+
4+
--echo # Bug#29998457 MYSQLDUMP GENERATES INVALID INSERT STATEMENTS WITH VARBINARY COLUMNS
5+
6+
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE = '';
7+
let $mysqldumpfile = $MYSQLTEST_VARDIR/tmp/bug29998457.sql;
8+
CREATE DATABASE test_bug29998457;
9+
USE test_bug29998457;
10+
11+
--echo #Pre-test cleanup
12+
--disable_warnings
13+
DROP TABLE IF EXISTS t1;
14+
--enable_warnings
15+
16+
CREATE TABLE t1 (
17+
pk INT,
18+
v1 VARCHAR(255),
19+
v2 VARCHAR(255),
20+
v3 VARCHAR(255),
21+
v4 VARCHAR(128),
22+
v5 VARCHAR(64),
23+
v6 VARCHAR(16),
24+
v7 VARCHAR(16),
25+
v8 VARCHAR(8),
26+
v9 VARBINARY(32),
27+
v10 VARBINARY(32));
28+
29+
SELECT '12345678901234567890123456789012345678901234567890' INTO @s;
30+
31+
SELECT CONCAT(@s, @s, @s, @s, @s) INTO @s1;
32+
33+
INSERT INTO t1 VALUES (1, @s1, @s1, @s1, @s1, @s1, @s1, @s1, @s1, '','NULL');
34+
35+
SELECT * FROM test_bug29998457.t1;
36+
37+
--exec $MYSQL_DUMP --skip-comments test_bug29998457 > $mysqldumpfile
38+
--exec $MYSQL --show-warnings test_bug29998457 < $mysqldumpfile
39+
40+
# Ensure that there were no invalid insert statements generated in the mysqldump
41+
# output and the dump was restored properly.
42+
SELECT * FROM test_bug29998457.t1;
43+
44+
#Cleanup
45+
--remove_file $mysqldumpfile
46+
DROP DATABASE test_bug29998457;
47+
SET SQL_MODE = @OLD_SQL_MODE;

0 commit comments

Comments
 (0)