From a5ef74e7eb4bab09c9bda4fcd7fab12302526ea4 Mon Sep 17 00:00:00 2001
From: Alexander Barkov <bar@mariadb.com>
Date: Wed, 22 Dec 2021 13:12:40 +0400
Subject: [PATCH] MDEV-27195 SIGSEGV in
 Table_scope_and_contents_source_st::vers_check_system_fields

The old code erroneously used default_charset_info to compare field names.
default_charset_info can point to any arbitrary collation,
including ucs2*, utf16*, utf32*, including those that do not
support strcasecmp().

my_charset_utf8mb4_unicode_ci, which is used in this scenario:

CREATE TABLE t1 ENGINE=InnoDB WITH SYSTEM VERSIONING AS SELECT 0;

does not support strcasecmp().

Fixing the code to use Lex_ident::streq(), which uses
system_charset_info instead of default_charset_info.
---
 mysql-test/main/ctype_utf8mb4_unicode_ci_def.opt  |  1 +
 .../main/ctype_utf8mb4_unicode_ci_def.result      | 11 +++++++++++
 mysql-test/main/ctype_utf8mb4_unicode_ci_def.test | 15 +++++++++++++++
 sql/handler.cc                                    |  3 +--
 4 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 mysql-test/main/ctype_utf8mb4_unicode_ci_def.opt
 create mode 100644 mysql-test/main/ctype_utf8mb4_unicode_ci_def.result
 create mode 100644 mysql-test/main/ctype_utf8mb4_unicode_ci_def.test

diff --git a/mysql-test/main/ctype_utf8mb4_unicode_ci_def.opt b/mysql-test/main/ctype_utf8mb4_unicode_ci_def.opt
new file mode 100644
index 00000000000..e430a45c10e
--- /dev/null
+++ b/mysql-test/main/ctype_utf8mb4_unicode_ci_def.opt
@@ -0,0 +1 @@
+--character-set-server=utf8mb4,latin1 --collation-server=utf8mb4_unicode_ci
diff --git a/mysql-test/main/ctype_utf8mb4_unicode_ci_def.result b/mysql-test/main/ctype_utf8mb4_unicode_ci_def.result
new file mode 100644
index 00000000000..2e15931248b
--- /dev/null
+++ b/mysql-test/main/ctype_utf8mb4_unicode_ci_def.result
@@ -0,0 +1,11 @@
+#
+# Start of 10.3 tests
+#
+#
+# MDEV-27195 SIGSEGV in Table_scope_and_contents_source_st::vers_check_system_fields
+#
+CREATE TABLE t1 ENGINE=MyISAM WITH SYSTEM VERSIONING AS SELECT 0;
+DROP TABLE t1;
+#
+# End of 10.3 tests
+#
diff --git a/mysql-test/main/ctype_utf8mb4_unicode_ci_def.test b/mysql-test/main/ctype_utf8mb4_unicode_ci_def.test
new file mode 100644
index 00000000000..fb7fbe04e3b
--- /dev/null
+++ b/mysql-test/main/ctype_utf8mb4_unicode_ci_def.test
@@ -0,0 +1,15 @@
+--echo #
+--echo # Start of 10.3 tests
+--echo #
+
+--echo #
+--echo # MDEV-27195 SIGSEGV in Table_scope_and_contents_source_st::vers_check_system_fields
+--echo #
+
+CREATE TABLE t1 ENGINE=MyISAM WITH SYSTEM VERSIONING AS SELECT 0;
+DROP TABLE t1;
+
+
+--echo #
+--echo # End of 10.3 tests
+--echo #
diff --git a/sql/handler.cc b/sql/handler.cc
index 11a387fb4e3..f8702c27a39 100644
--- a/sql/handler.cc
+++ b/sql/handler.cc
@@ -7264,8 +7264,7 @@ bool Table_scope_and_contents_source_st::vers_check_system_fields(
       {
         List_iterator<Create_field> dup_it(alter_info->create_list);
         for (Create_field *dup= dup_it++; !is_dup && dup != f; dup= dup_it++)
-          is_dup= my_strcasecmp(default_charset_info,
-                                dup->field_name.str, f->field_name.str) == 0;
+          is_dup= Lex_ident(dup->field_name).streq(f->field_name);
       }
 
       if (!(f->flags & VERS_UPDATE_UNVERSIONED_FLAG) && !is_dup)