From 2ccae65cff59c0e65402291ca7eb52caa6d152fd Mon Sep 17 00:00:00 2001
From: Varun Gupta <varun.gupta@mariadb.com>
Date: Fri, 24 Aug 2018 17:00:32 +0530
Subject: [PATCH] Fixed ASAN failure for the test main.func_misc

Moved the checks for arguments validation of Item_name_const from the constructor
to Create_func_name_const::create_2_arg
Also reverted the fix bf1c53e9be84437ada32393bb7b4a8ff06dbf369
---
 sql/item.cc        | 19 -------------------
 sql/item.h         |  2 --
 sql/item_create.cc | 21 ++++++++++++++++++++-
 sql/sql_yacc.yy    |  5 ++---
 4 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/sql/item.cc b/sql/item.cc
index 77e751d7789..7171a95c21d 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -1960,25 +1960,6 @@ Item_name_const::Item_name_const(THD *thd, Item *name_arg, Item *val):
   Item_fixed_hybrid(thd), value_item(val), name_item(name_arg)
 {
   Item::maybe_null= TRUE;
-  if (!name_item->basic_const_item())
-    goto err;
-
-  if (value_item->basic_const_item())
-    return; // ok
-
-  if (value_item->type() == FUNC_ITEM)
-  {
-    Item_func *value_func= (Item_func *) value_item;
-    if (value_func->functype() != Item_func::COLLATE_FUNC &&
-        value_func->functype() != Item_func::NEG_FUNC)
-      goto err;
-
-    if (value_func->key_item()->basic_const_item())
-      return; // ok
-  }
-
-err:
-  my_error(ER_WRONG_ARGUMENTS, MYF(0), "NAME_CONST");
 }
 
 
diff --git a/sql/item.h b/sql/item.h
index 6f1f70c3cc7..c013781f30f 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -881,7 +881,6 @@ class Item: public Value_source,
   */
   String *val_str() { return val_str(&str_value); }
   virtual Item_func *get_item_func() { return NULL; }
-  virtual Item_field *get_item_field() {return NULL;}
 
   const MY_LOCALE *locale_from_val_str();
 
@@ -3262,7 +3261,6 @@ class Item_field :public Item_ident,
   longlong val_int_endpoint(bool left_endp, bool *incl_endp);
   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
   bool get_date_result(MYSQL_TIME *ltime,ulonglong fuzzydate);
-  Item_field* get_item_field() {return this;}
   bool is_null() { return field->is_null(); }
   void update_null_value();
   void update_table_bitmaps()
diff --git a/sql/item_create.cc b/sql/item_create.cc
index d9b007d4728..87bf69f3c96 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -6103,7 +6103,26 @@ Create_func_name_const Create_func_name_const::s_singleton;
 Item*
 Create_func_name_const::create_2_arg(THD *thd, Item *arg1, Item *arg2)
 {
-  return new (thd->mem_root) Item_name_const(thd, arg1, arg2);
+  if (!arg1->basic_const_item())
+    goto err;
+
+  if (arg2->basic_const_item())
+    return new (thd->mem_root) Item_name_const(thd, arg1, arg2);
+
+  if (arg2->type() == Item::FUNC_ITEM)
+  {
+    Item_func *value_func= (Item_func *) arg2;
+    if (value_func->functype() != Item_func::COLLATE_FUNC &&
+        value_func->functype() != Item_func::NEG_FUNC)
+      goto err;
+
+    if (!value_func->key_item()->basic_const_item())
+      goto err;
+    return new (thd->mem_root) Item_name_const(thd, arg1, arg2);
+  }
+err:
+  my_error(ER_WRONG_ARGUMENTS, MYF(0), "NAME_CONST");
+  return NULL;
 }
 
 
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index bb47988e3c0..cf894325ba5 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -292,14 +292,13 @@ LEX::set_system_variable(enum enum_var_type var_type,
                          Item *val)
 {
   set_var *setvar;
-  Item_field *item_field;
 
   /* No AUTOCOMMIT from a stored function or trigger. */
   if (spcont && sysvar == Sys_autocommit_ptr)
     sphead->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
 
-  if (val && (item_field= val->get_item_field()) &&
-      item_field->table_name)
+  if (val && val->type() == Item::FIELD_ITEM &&
+      ((Item_field*)val)->table_name)
   {
     my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), sysvar->name.str);
     return TRUE;
-- 
2.30.9