Bug #122071 mysqldump --tab --default-character-set=utf8mb4 cuts BLOB/VARBINARY/BINARY/BIT values at their first invalid byte

Submitted: 5 Dec 2:48
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: mysqldump Command-line Client Severity:S2 (Serious)
Version:26.10.0.1, 8.4.13.1 OS:Any
Assigned to: CPU Architecture:Any

[5 Dec 2:48] Ke Han
Description:
mysqldump --tab with an explicit --default-character-set writes every
BINARY, VARBINARY, BLOB and BIT value cut at its first byte that is not
valid in that character set. This happens even with utf8mb4, which is the
documented default ("If no character set is specified, mysqldump uses
utf8mb4"). BINARY(16) UUIDs from UUID_TO_BIN() keep 1 or 4 of their 16
bytes, a PNG header becomes an empty string, and a BIT(16) value becomes 0.
mysqldump prints nothing, and mysqlimport reports "Records: 3 Deleted: 0
Skipped: 0 Warnings: 0". Without the option, the same dump is correct. The
manual offers no way around it: "The --hex-blob option is ignored when the
--tab is used" (Bug #32941, Verified 2007, asks for --hex-blob with --tab).
Any explicit value except a single-byte set such as latin1 cuts values;
utf8mb4, utf8mb3, sjis and gbk were tested.

Two defects combine:

1. mysqldump treats an explicit --default-character-set=utf8mb4 as not the
   default because it compares pointers, not names. default_charset starts
   as the address of the constant mysql_universal_client_charset
   (client/mysqldump.cc lines 256 and 6911). The --tab query appends
   "CHARACTER SET binary" only while default_charset ==
   mysql_universal_client_charset, and otherwise the given name
   (mysqldump.cc line 4427, "backward compatibility"). An explicit utf8mb4
   is another pointer, so the file is requested in utf8mb4.

2. The server "converts" binary strings to the file's character set by
   validating them. Query_result_export::send_data() converts every value
   whose character set differs from the file's
   (sql/query_result.cc line 690:
     if (res && !my_charset_same(write_cs, res->charset()) &&
         !my_charset_same(write_cs, &my_charset_bin))),
   including values of character set binary. well_formed_copy_nchars()
   copies only the prefix that is well-formed in the target set; the export
   pushes warning 1366 and writes the cut value (cvt_str.length(bytes)).
   mysqldump never reads the warnings of its SELECT ... INTO OUTFILE.

On its own, SELECT uuid INTO OUTFILE '...' CHARACTER SET utf8mb4 FROM t gives
"Warning 1366 Incorrect string value: '\xCDx\x0C\xBA\xBA\x10...' for column
'uuid' at row 1" and writes the cut value.

Bug #53088 (closed 2010) was the same conversion cutting long TEXT/BLOB
values to 766 bytes. Its reporter asked for binary columns not to be
converted at all; the fix enlarged the buffer and kept the conversion.

How to repeat:
On the server host (the files go to secure_file_priv, /var/lib/mysql-files
in the official image):

mysql -uroot <<'SQL'
CREATE DATABASE src;
CREATE TABLE src.t (id INT PRIMARY KEY, b BLOB, vb VARBINARY(16), uuid BINARY(16), bt BIT(16), txt VARCHAR(20));
INSERT INTO src.t VALUES
 (1, x'89504E470D0A1A0A0000000D49484452', x'FF00', UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'), b'1111111100000001', 'png header'),
 (2, x'616263FF646566', x'E29C93', UUID_TO_BIN('00000000-0000-0000-0000-000000000000'), b'0000000001000001', 'abc'),
 (3, 'only ascii', 'ascii', UUID_TO_BIN('123e4567-e89b-12d3-a456-426614174000'), b'0', 'ok');
CREATE DATABASE dst;
SQL
mysqldump -uroot --tab=/var/lib/mysql-files --default-character-set=utf8mb4 src t
mysql -uroot dst < /var/lib/mysql-files/t.sql
mysqlimport -uroot dst /var/lib/mysql-files/t.txt
mysql -uroot --table <<'SQL'
SELECT s.id, HEX(s.b), HEX(d.b), HEX(s.vb), HEX(d.vb), HEX(s.uuid), HEX(d.uuid), HEX(s.bt), HEX(d.bt)
  FROM src.t s JOIN dst.t d USING (id) ORDER BY id;
SQL

Result on 26.10.0.1 (8.4.13.1 gives the same values):
mysqldump prints only its usual GTID warnings; mysqlimport prints
"dst.t: Records: 3  Deleted: 0  Skipped: 0  Warnings: 0".
+----+----------------------------------+----------------------+------------+------------+----------------------------------+----------------------------------+-----------+-----------+
| id | HEX(s.b)                         | HEX(d.b)             | HEX(s.vb)  | HEX(d.vb)  | HEX(s.uuid)                      | HEX(d.uuid)                      | HEX(s.bt) | HEX(d.bt) |
+----+----------------------------------+----------------------+------------+------------+----------------------------------+----------------------------------+-----------+-----------+
|  1 | 89504E470D0A1A0A0000000D49484452 |                      | FF00       |            | 6CCD780CBABA102695645B8C656024DB | 6C000000000000000000000000000000 | FF01      | 0         |
|  2 | 616263FF646566                   | 616263               | E29C93     | E29C93     | 00000000000000000000000000000000 | 00000000000000000000000000000000 | 41        | 41        |
|  3 | 6F6E6C79206173636969             | 6F6E6C79206173636969 | 6173636969 | 6173636969 | 123E4567E89B12D3A456426614174000 | 123E4567000000000000000000000000 | 0         | 0         |
+----+----------------------------------+----------------------+------------+------------+----------------------------------+----------------------------------+-----------+-----------+

The same commands without --default-character-set=utf8mb4 give 3 of 3
identical rows.

Expected: the same files as without the option (which is the documented
default), so that the reload is identical; at least an error or warning
instead of silently cut values.

Suggested fix:
- Server: write values of character set binary without conversion in
  Query_result_export::send_data(), as Bug #53088's reporter proposed;
  converting them cannot produce anything but a cut copy.
- mysqldump: compare the character set names (my_strcasecmp) instead of the
  pointers, so that an explicit utf8mb4 behaves like the default; and check
  the warnings of the SELECT ... INTO OUTFILE it sends.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.039 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.