diff --git a/mysql-test/r/explain.result b/mysql-test/r/explain.result index 742dc5ae49d..ee3a4ba3387 100644 --- a/mysql-test/r/explain.result +++ b/mysql-test/r/explain.result @@ -155,3 +155,18 @@ id select_type table type possible_keys key key_len ref rows filtered Extra Warnings: Note 1003 select 1 AS `1` from (select count(distinct `test`.`t1`.`a`) AS `COUNT(DISTINCT t1.a)` from `test`.`t1` join `test`.`t2` group by `test`.`t1`.`a`) `s1` DROP TABLE t1,t2; +# +# Bug#37870: Usage of uninitialized value caused failed assertion. +# +create table t1 (dt datetime not null); +create table t2 (dt datetime not null); +insert into t1 values ('2001-01-01 1:1:1'), ('2001-01-01 1:1:1'); +insert into t2 values ('2001-01-01 1:1:1'), ('2001-01-01 1:1:1'); +flush tables; +EXPLAIN SELECT OUTR.dt FROM t1 AS OUTR WHERE OUTR.dt IN (SELECT INNR.dt FROM t2 AS INNR WHERE OUTR.dt IS NULL ); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY OUTR ALL NULL NULL NULL NULL 2 Using where +2 DEPENDENT SUBQUERY INNR ALL NULL NULL NULL NULL 2 Using where +SELECT OUTR.dt FROM t1 AS OUTR WHERE OUTR.dt IN (SELECT INNR.dt FROM t2 AS INNR WHERE OUTR.dt IS NULL ); +dt +drop tables t1, t2; diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index a0af9fdcf1d..90530462732 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -3672,6 +3672,8 @@ DROP VIEW v1; # -- End of test case for Bug#35193. +CREATE VIEW v1 AS SELECT 1; +DROP VIEW v1; # ----------------------------------------------------------------- # -- End of 5.0 tests. # ----------------------------------------------------------------- diff --git a/mysql-test/t/explain.test b/mysql-test/t/explain.test index 0247aca82df..e94f6d4d87d 100644 --- a/mysql-test/t/explain.test +++ b/mysql-test/t/explain.test @@ -123,4 +123,16 @@ execute s1; DROP TABLE t1,t2; +--echo # +--echo # Bug#37870: Usage of uninitialized value caused failed assertion. +--echo # +create table t1 (dt datetime not null); +create table t2 (dt datetime not null); +insert into t1 values ('2001-01-01 1:1:1'), ('2001-01-01 1:1:1'); +insert into t2 values ('2001-01-01 1:1:1'), ('2001-01-01 1:1:1'); +flush tables; +EXPLAIN SELECT OUTR.dt FROM t1 AS OUTR WHERE OUTR.dt IN (SELECT INNR.dt FROM t2 AS INNR WHERE OUTR.dt IS NULL ); +SELECT OUTR.dt FROM t1 AS OUTR WHERE OUTR.dt IN (SELECT INNR.dt FROM t2 AS INNR WHERE OUTR.dt IS NULL ); +drop tables t1, t2; + # End of 5.0 tests. diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 7db1a693a93..05aa76d492d 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -3556,6 +3556,15 @@ DROP VIEW v1; ########################################################################### +# +# Bug#39040: valgrind errors/crash when creating views with binlog logging +# enabled +# +# Bug is visible only when running in valgrind with binary logging. +CREATE VIEW v1 AS SELECT 1; +DROP VIEW v1; + + --echo # ----------------------------------------------------------------- --echo # -- End of 5.0 tests. --echo # ----------------------------------------------------------------- diff --git a/sql/item_cmpfunc.cc b/sql/item_cmpfunc.cc index bd6065c9403..cefa479fea6 100644 --- a/sql/item_cmpfunc.cc +++ b/sql/item_cmpfunc.cc @@ -413,10 +413,11 @@ static bool convert_constant_item(THD *thd, Item_field *field_item, thd->count_cuted_fields= CHECK_FIELD_IGNORE; /* - Store the value of the field if it references an outer field because + Store the value of the field/constant if it references an outer field because the call to save_in_field below overrides that value. + Don't save value of the field for EXPLAIN since it's not initialized. */ - if (field_item->depended_from) + if (field_item->depended_from && (!thd->lex->describe || field_item->const_item())) orig_field_val= field->val_int(); if (!(*item)->is_null() && !(*item)->save_in_field(field, 1)) { @@ -427,7 +428,7 @@ static bool convert_constant_item(THD *thd, Item_field *field_item, result= 1; // Item was replaced } /* Restore the original field value. */ - if (field_item->depended_from) + if (field_item->depended_from && (!thd->lex->describe || field_item->const_item())) { result= field->store(orig_field_val, TRUE); /* orig_field_val must be a valid value that can be restored back. */ diff --git a/sql/sql_view.cc b/sql/sql_view.cc index 87b073af078..83e8d5907cf 100644 --- a/sql/sql_view.cc +++ b/sql/sql_view.cc @@ -816,13 +816,24 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view, DBUG_PRINT("info", ("View: %s", view_query.ptr())); /* fill structure */ - view->select_stmt.str= view_query.c_ptr_safe(); - view->select_stmt.length= view_query.length(); view->source= thd->lex->create_view_select; + if (!thd->make_lex_string(&view->select_stmt, view_query.ptr(), + view_query.length(), false)) + { + my_error(ER_OUT_OF_RESOURCES, MYF(0)); + error= -1; + goto err; + } + view->file_version= 1; view->calc_md5(md5); - view->md5.str= md5; + if (!(view->md5.str= (char*) thd->memdup(md5, 32))) + { + my_error(ER_OUT_OF_RESOURCES, MYF(0)); + error= -1; + goto err; + } view->md5.length= 32; can_be_merged= lex->can_be_merged(); if (lex->create_view_algorithm == VIEW_ALGORITHM_MERGE && @@ -949,8 +960,13 @@ loop_out: lex_string_set(&view->view_connection_cl_name, view->view_creation_ctx->get_connection_cl()->name); - view->view_body_utf8.str= is_query.c_ptr_safe(); - view->view_body_utf8.length= is_query.length(); + if (!thd->make_lex_string(&view->view_body_utf8, is_query.ptr(), + is_query.length(), false)) + { + my_error(ER_OUT_OF_RESOURCES, MYF(0)); + error= -1; + goto err; + } /* Check that table of main select do not used in subqueries.