Bug #32282 TEXT silently truncates when value is exactly 65536 bytes
Submitted: 12 Nov 2007 14:12 Modified: 18 Dec 2007 4:49
Reporter: Roland Bouman Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Data Types Severity:S3 (Non-critical)
Version:5.0, 5.1.22 rc OS:Linux (kubuntu feisty)
Assigned to: Gleb Shchepa CPU Architecture:Any

[12 Nov 2007 14:12] Roland Bouman
Description:
TEXT is supposed to be able to store 65535 bytes at a maximum. It truncates the value if it exceeds that amount and gives a warning.

However,there is a strange corner case when the length is exactly 65536 bytes, in which case the value is *silently truncated* to the actual maximum of 65535 bytes.

How to repeat:
mysql> create table t(
    ->     c text charset ascii
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql>
mysql> insert into t (c) values (repeat('1',65537));
Query OK, 1 row affected, 1 warning (0.01 sec)

Warning (Code 1265): Data truncated for column 'c' at row 1
mysql>
mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'c' at row 1 |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)

mysql>
mysql> insert into t (c) values (repeat('2',65536));
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> show warnings;
Empty set (0.00 sec)

mysql>
mysql> insert into t (c) values (repeat('3',65535));
Query OK, 1 row affected (0.03 sec)

mysql>
mysql> show warnings;
Empty set (0.00 sec)

mysql>
mysql> select length(c), char_length(c)
    -> from t;
+-----------+----------------+
| length(c) | char_length(c) |
+-----------+----------------+
|     65535 |          65535 |
|     65535 |          65535 |
|     65535 |          65535 |
+-----------+----------------+
3 rows in set (0.00 sec)

Suggested fix:
Please don't truncate and give a proper warning (or error depedning on sql_mode)
[12 Nov 2007 16:07] Valeriy Kravchuk
Thank you for a bug report. Verified just as described.
[12 Nov 2007 16:15] Hartmut Holzgraefe
mysqltest test case

Attachment: bug32282.tgz (application/x-gtar, text), 880 bytes.

[12 Nov 2007 21:09] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/37617

ChangeSet@1.2570, 2007-11-13 01:10:30+04:00, gshchepa@gleb.loc +3 -0
  Fixed bug #32282: TEXT silently truncates when value is exactly 65536
  bytes length.
  
  The server has been modified to report warnings on truncation to
  65536 bytes as usual.
[12 Nov 2007 23:24] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/37625

ChangeSet@1.2570, 2007-11-13 03:26:03+04:00, gshchepa@gleb.loc +3 -0
  Fixed bug #32282: TEXT silently truncates when value is exactly 65536
  bytes length.
  
  The server has been modified to report warnings on truncation to
  65536 bytes as usual.
  
  Byte length of the input string was used in the
  Field_blob::store method instead of the maximal number
  of characters (see the well_formed_copy_nchars function call).
[19 Nov 2007 17:25] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/38086

ChangeSet@1.2570, 2007-11-19 21:25:36+04:00, gshchepa@gleb.loc +3 -0
  Fixed bug #32282: TEXT silently truncates when value is exactly 65536
  bytes length.
  
  The server has been modified to report warnings on truncation to
  65536 bytes as usual.
[14 Dec 2007 8:14] Bugs System
Pushed into 5.0.54
[14 Dec 2007 8:17] Bugs System
Pushed into 5.1.23-rc
[14 Dec 2007 8:21] Bugs System
Pushed into 6.0.5-alpha
[18 Dec 2007 4:49] Paul DuBois
Noted in 5.0.54, 5.1.23, 6.0.5 changelogs.
[18 Dec 2007 4:50] Paul DuBois
Sorry, forgot changelog text:

Assigning a 65,536-byte string to a TEXT column (which can hold a
maximum of 65,535 bytes) resulted in truncation without a warning.
Now a truncation warning is generated.