Skip to content

Commit a270e8a

Browse files
author
unknown
committed
MDEV-6441: memory leak
mysql_derived_prepare() was executed on the statement memory. Now it is executed on the runtime memory. All bugs induced by this were fixed.
1 parent 5364315 commit a270e8a

File tree

5 files changed

+122
-7
lines changed

5 files changed

+122
-7
lines changed

mysql-test/r/view.result

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5302,6 +5302,61 @@ NULL 8
53025302
drop view v1;
53035303
drop table t1,t2,t3;
53045304
SET optimizer_switch=@save_optimizer_switch_MDEV_3874;
5305+
CREATE TABLE `t1` (
5306+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
5307+
`f0` int(11) unsigned NOT NULL DEFAULT '0',
5308+
`f1` int(11) unsigned NOT NULL DEFAULT '0',
5309+
PRIMARY KEY (`id`),
5310+
UNIQUE KEY `id` (`id`)
5311+
);
5312+
CREATE TABLE `t2` (
5313+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
5314+
`f02` bigint(20) unsigned NOT NULL DEFAULT '0',
5315+
`f03` int(11) unsigned NOT NULL DEFAULT '0',
5316+
PRIMARY KEY (`id`),
5317+
UNIQUE KEY `id` (`id`)
5318+
);
5319+
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `v1` AS
5320+
SELECT
5321+
`t1`.`f0` AS `f0`,
5322+
`t1`.`f1` AS `f1`,
5323+
`t2`.`f02` AS `f02`,
5324+
`t2`.`f03` AS `f03`
5325+
FROM
5326+
(`t1` LEFT JOIN `t2` ON((`t1`.`id` = `t2`.`f02`)));
5327+
CREATE FUNCTION `f1`(
5328+
p0 BIGINT(20) UNSIGNED
5329+
)
5330+
RETURNS bigint(20) unsigned
5331+
DETERMINISTIC
5332+
CONTAINS SQL
5333+
SQL SECURITY DEFINER
5334+
COMMENT ''
5335+
BEGIN
5336+
DECLARE k0 INTEGER UNSIGNED DEFAULT 0;
5337+
DECLARE lResult INTEGER UNSIGNED DEFAULT 0;
5338+
SET k0 = 0;
5339+
WHILE k0 < 1 DO
5340+
SELECT COUNT(*) as `f00` INTO lResult FROM `v1` WHERE `v1`.`f0` = p0; -- BUG
5341+
SET k0 = k0 + 1;
5342+
END WHILE;
5343+
RETURN(k0);
5344+
END|
5345+
SELECT `f1`(1);
5346+
`f1`(1)
5347+
1
5348+
SELECT `f1`(1);
5349+
`f1`(1)
5350+
1
5351+
SELECT `f1`(1);
5352+
`f1`(1)
5353+
1
5354+
SELECT `f1`(1);
5355+
`f1`(1)
5356+
1
5357+
DROP FUNCTION f1;
5358+
DROP VIEW v1;
5359+
DROP TABLE t1, t2;
53055360
# -----------------------------------------------------------------
53065361
# -- End of 5.5 tests.
53075362
# -----------------------------------------------------------------

mysql-test/t/view.test

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5231,6 +5231,69 @@ drop view v1;
52315231
drop table t1,t2,t3;
52325232
SET optimizer_switch=@save_optimizer_switch_MDEV_3874;
52335233

5234+
#
5235+
# MDEV-5515: sub-bug test of 3rd execution crash
5236+
#
5237+
5238+
CREATE TABLE `t1` (
5239+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
5240+
`f0` int(11) unsigned NOT NULL DEFAULT '0',
5241+
`f1` int(11) unsigned NOT NULL DEFAULT '0',
5242+
PRIMARY KEY (`id`),
5243+
UNIQUE KEY `id` (`id`)
5244+
);
5245+
5246+
CREATE TABLE `t2` (
5247+
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
5248+
`f02` bigint(20) unsigned NOT NULL DEFAULT '0',
5249+
`f03` int(11) unsigned NOT NULL DEFAULT '0',
5250+
PRIMARY KEY (`id`),
5251+
UNIQUE KEY `id` (`id`)
5252+
);
5253+
5254+
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW `v1` AS
5255+
SELECT
5256+
`t1`.`f0` AS `f0`,
5257+
`t1`.`f1` AS `f1`,
5258+
`t2`.`f02` AS `f02`,
5259+
`t2`.`f03` AS `f03`
5260+
FROM
5261+
(`t1` LEFT JOIN `t2` ON((`t1`.`id` = `t2`.`f02`)));
5262+
5263+
--delimiter |
5264+
CREATE FUNCTION `f1`(
5265+
p0 BIGINT(20) UNSIGNED
5266+
)
5267+
RETURNS bigint(20) unsigned
5268+
DETERMINISTIC
5269+
CONTAINS SQL
5270+
SQL SECURITY DEFINER
5271+
COMMENT ''
5272+
BEGIN
5273+
5274+
DECLARE k0 INTEGER UNSIGNED DEFAULT 0;
5275+
DECLARE lResult INTEGER UNSIGNED DEFAULT 0;
5276+
5277+
SET k0 = 0;
5278+
WHILE k0 < 1 DO
5279+
SELECT COUNT(*) as `f00` INTO lResult FROM `v1` WHERE `v1`.`f0` = p0; -- BUG
5280+
SET k0 = k0 + 1;
5281+
END WHILE;
5282+
5283+
RETURN(k0);
5284+
END|
5285+
--delimiter ;
5286+
5287+
5288+
SELECT `f1`(1);
5289+
SELECT `f1`(1);
5290+
SELECT `f1`(1);
5291+
SELECT `f1`(1);
5292+
5293+
DROP FUNCTION f1;
5294+
DROP VIEW v1;
5295+
DROP TABLE t1, t2;
5296+
52345297
--echo # -----------------------------------------------------------------
52355298
--echo # -- End of 5.5 tests.
52365299
--echo # -----------------------------------------------------------------

sql/sql_derived.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
614614
SELECT_LEX_UNIT *unit= derived->get_unit();
615615
DBUG_ENTER("mysql_derived_prepare");
616616
bool res= FALSE;
617+
DBUG_PRINT("enter", ("unit 0x%lx", (ulong) unit));
617618

618619
// Skip already prepared views/DT
619620
if (!unit || unit->prepared ||
@@ -623,9 +624,6 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
623624
thd->lex->sql_command == SQLCOM_DELETE_MULTI))))
624625
DBUG_RETURN(FALSE);
625626

626-
Query_arena *arena, backup;
627-
arena= thd->activate_stmt_arena_if_needed(&backup);
628-
629627
SELECT_LEX *first_select= unit->first_select();
630628

631629
/* prevent name resolving out of derived table */
@@ -743,8 +741,6 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
743741
if (derived->outer_join)
744742
table->maybe_null= 1;
745743
}
746-
if (arena)
747-
thd->restore_active_arena(arena, &backup);
748744
DBUG_RETURN(res);
749745
}
750746

sql/sql_lex.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3298,7 +3298,7 @@ static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
32983298
{
32993299
for (; tbl; tbl= tbl->next_local)
33003300
{
3301-
if (tbl->on_expr)
3301+
if (tbl->on_expr && !tbl->prep_on_expr)
33023302
{
33033303
thd->check_and_register_item_tree(&tbl->prep_on_expr, &tbl->on_expr);
33043304
tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);

sql/table.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4140,7 +4140,8 @@ bool TABLE_LIST::create_field_translation(THD *thd)
41404140

41414141
while ((item= it++))
41424142
{
4143-
transl[field_count].name= item->name;
4143+
DBUG_ASSERT(item->name && item->name[0]);
4144+
transl[field_count].name= thd->strdup(item->name);
41444145
transl[field_count++].item= item;
41454146
}
41464147
field_translation= transl;

0 commit comments

Comments
 (0)