From f5b0dd6a002846a5c3d3bdc1651e51aad1ce2ddc Mon Sep 17 00:00:00 2001
From: "gkodinov/kgeorge@macbook.gmz" <>
Date: Tue, 25 Jul 2006 18:42:49 +0300
Subject: [PATCH] Bug #21086: server crashes when VIEW defined with a SELECT
 with COLLATE clause is called   When executing INSERT over a view with
 calculated columns it was assuming all   elements of the fields collection
 are actually Item_field instances.   This may not be true when inserting into
 a view and that view has columns that are   such expressions that allow
 updating (like setting a collation for example).   Corrected to access field
 information through the filed_for_view_update() function and   retrieve
 correctly the field info even for "update-friendly" non-Item_field items.

---
 mysql-test/r/view.result | 13 +++++++++++++
 mysql-test/t/view.test   | 19 +++++++++++++++++++
 sql/item_strfunc.h       |  2 +-
 sql/sql_base.cc          |  9 +++++++--
 4 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result
index c8a673e2209..b2c65423b59 100644
--- a/mysql-test/r/view.result
+++ b/mysql-test/r/view.result
@@ -2807,3 +2807,16 @@ yadda
 yad
 DROP VIEW v1;
 DROP TABLE t1;
+CREATE TABLE t1 (s1 char);
+INSERT INTO t1 VALUES ('Z');
+CREATE VIEW v1 AS SELECT s1 collate latin1_german1_ci AS col FROM t1;
+CREATE VIEW v2 (col) AS SELECT s1 collate latin1_german1_ci FROM t1;
+INSERT INTO v1 (col) VALUES ('b');
+INSERT INTO v2 (col) VALUES ('c');
+SELECT s1 FROM t1;
+s1
+Z
+b
+c
+DROP VIEW v1, v2;
+DROP TABLE t1;
diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test
index 6399cef9086..1b930353ca4 100644
--- a/mysql-test/t/view.test
+++ b/mysql-test/t/view.test
@@ -2667,3 +2667,22 @@ SELECT * FROM v1;
 DROP VIEW v1;
 
 DROP TABLE t1;
+
+#
+# Bug #21086: server crashes when VIEW defined with a SELECT with COLLATE 
+# clause is called
+#
+CREATE TABLE t1 (s1 char);
+INSERT INTO t1 VALUES ('Z');
+
+CREATE VIEW v1 AS SELECT s1 collate latin1_german1_ci AS col FROM t1;
+
+CREATE VIEW v2 (col) AS SELECT s1 collate latin1_german1_ci FROM t1;
+
+# either of these statements will cause crash
+INSERT INTO v1 (col) VALUES ('b');
+INSERT INTO v2 (col) VALUES ('c');
+
+SELECT s1 FROM t1;
+DROP VIEW v1, v2;
+DROP TABLE t1;
diff --git a/sql/item_strfunc.h b/sql/item_strfunc.h
index a72182abcf7..488dc20b063 100644
--- a/sql/item_strfunc.h
+++ b/sql/item_strfunc.h
@@ -728,7 +728,7 @@ class Item_func_set_collation :public Item_str_func
   void fix_length_and_dec();
   bool eq(const Item *item, bool binary_cmp) const;
   const char *func_name() const { return "collate"; }
-  enum Functype func_type() const { return COLLATE_FUNC; }
+  enum Functype functype() const { return COLLATE_FUNC; }
   void print(String *str);
   Item_field *filed_for_view_update()
   {
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index 7f9076bb46e..28edee5c729 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -4959,12 +4959,17 @@ fill_record(THD * thd, List<Item> &fields, List<Item> &values,
             bool ignore_errors)
 {
   List_iterator_fast<Item> f(fields),v(values);
-  Item *value;
+  Item *value, *fld;
   Item_field *field;
   DBUG_ENTER("fill_record");
 
-  while ((field=(Item_field*) f++))
+  while ((fld= f++))
   {
+    if (!(field= fld->filed_for_view_update()))
+    {
+      my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), fld->name);
+      DBUG_RETURN(TRUE);
+    }
     value=v++;
     Field *rfield= field->field;
     TABLE *table= rfield->table;
-- 
2.30.9