From eb41c1171e34d5d69af6f9f87fa2e41db6018221 Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Tue, 5 Nov 2024 11:16:10 +0400 Subject: [PATCH] MDEV-33942 View cuts off the end of string with the utf8 character set in INSERT function Item_func_insert::fix_length_and_dec() incorrectly calculated max_length when its collation.collation evaluated to my_charset_bin. Fixing the code to calculate max_length in terms of octets rather than in terms of characters when collation.collation is my_charset_bin. --- mysql-test/main/func_str.result | 11 +++++++++++ mysql-test/main/func_str.test | 10 ++++++++++ sql/item_strfunc.cc | 8 ++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/mysql-test/main/func_str.result b/mysql-test/main/func_str.result index a15a253df0c..68cda5fae95 100644 --- a/mysql-test/main/func_str.result +++ b/mysql-test/main/func_str.result @@ -5344,5 +5344,16 @@ left('hello', 4294967295) hello drop view v1; # +# MDEV-33942 View cuts off the end of string with the utf8 character set in INSERT function +# +SELECT HEX(INSERT(_utf8 0xD18FD18E, 2, 1, 0x20)); +HEX(INSERT(_utf8 0xD18FD18E, 2, 1, 0x20)) +D120D18E +CREATE VIEW v1 AS SELECT HEX(INSERT(_utf8 0xD18FD18E, 2, 1, 0x20)); +SELECT * FROM v1; +HEX(INSERT(_utf8 0xD18FD18E, 2, 1, 0x20)) +D120D18E +DROP VIEW v1; +# # End of 10.6 tests # diff --git a/mysql-test/main/func_str.test b/mysql-test/main/func_str.test index ecbc1157c87..d632250ba52 100644 --- a/mysql-test/main/func_str.test +++ b/mysql-test/main/func_str.test @@ -2393,6 +2393,16 @@ create view v1 as select left('hello', 4294967295); select * from v1; drop view v1; +--echo # +--echo # MDEV-33942 View cuts off the end of string with the utf8 character set in INSERT function +--echo # + +SELECT HEX(INSERT(_utf8 0xD18FD18E, 2, 1, 0x20)); +CREATE VIEW v1 AS SELECT HEX(INSERT(_utf8 0xD18FD18E, 2, 1, 0x20)); +SELECT * FROM v1; +DROP VIEW v1; + + --echo # --echo # End of 10.6 tests --echo # diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index b4ccc2611a3..e6fb3e8ba83 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1562,8 +1562,12 @@ bool Item_func_insert::fix_length_and_dec() // Handle character set for args[0] and args[3]. if (agg_arg_charsets_for_string_result(collation, args, 2, 3)) return TRUE; - char_length= ((ulonglong) args[0]->max_char_length() + - (ulonglong) args[3]->max_char_length()); + if (collation.collation == &my_charset_bin) + char_length= (ulonglong) args[0]->max_length + + (ulonglong) args[3]->max_length; + else + char_length= ((ulonglong) args[0]->max_char_length() + + (ulonglong) args[3]->max_char_length()); fix_char_length_ulonglong(char_length); return FALSE; }