From 6ac1d882a12961ae8aa171f396a3e79870ddfda7 Mon Sep 17 00:00:00 2001 From: Dmitry Shulga Date: Wed, 19 Jul 2023 18:06:59 +0700 Subject: [PATCH] MDEV-5816: Stored programs: validation of stored program statements Fixed memory leakage taken place on execution of the statement SHOW CREATE PACKAGE `pkg_name` The memory leak was caused by implementation of sp_compile() where a memory root for a stored routine was allocated but a pointer to the new memory root wasn't passed to sp_package::create for subsequent forwarding to the constructor of sp_package. Instead, another one memory root was allocated and the pointer to the original memory root was missed. --- sql/sp_head.cc | 14 +++++++++----- sql/sp_head.h | 2 +- sql/sql_lex.cc | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/sql/sp_head.cc b/sql/sp_head.cc index 7cb82d8f41c..22484f5f996 100644 --- a/sql/sp_head.cc +++ b/sql/sp_head.cc @@ -607,14 +607,18 @@ sp_head::sp_head(MEM_ROOT *mem_root_arg, sp_package *parent, sp_package *sp_package::create(LEX *top_level_lex, const sp_name *name, - const Sp_handler *sph) + const Sp_handler *sph, MEM_ROOT *sp_mem_root) { MEM_ROOT own_root; - init_sql_alloc(key_memory_sp_head_main_root, &own_root, MEM_ROOT_BLOCK_SIZE, - MEM_ROOT_PREALLOC, MYF(0)); + if (!sp_mem_root) + { + init_sql_alloc(key_memory_sp_head_main_root, &own_root, MEM_ROOT_BLOCK_SIZE, + MEM_ROOT_PREALLOC, MYF(0)); + sp_mem_root= &own_root; + } sp_package *sp; - if (!(sp= new (&own_root) sp_package(&own_root, top_level_lex, name, sph))) - free_root(&own_root, MYF(0)); + if (!(sp= new (sp_mem_root) sp_package(sp_mem_root, top_level_lex, name, sph))) + free_root(sp_mem_root, MYF(0)); return sp; } diff --git a/sql/sp_head.h b/sql/sp_head.h index e225da6b9c1..fb1841ff079 100644 --- a/sql/sp_head.h +++ b/sql/sp_head.h @@ -1061,7 +1061,7 @@ private: ~sp_package(); public: static sp_package *create(LEX *top_level_lex, const sp_name *name, - const Sp_handler *sph); + const Sp_handler *sph, MEM_ROOT *sp_mem_root); bool add_routine_declaration(LEX *lex) { diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 3ed16a2172a..5d22d9e056e 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -9386,7 +9386,8 @@ sp_package *LEX::create_package_start(THD *thd, return 0; } } - if (unlikely(!(pkg= sp_package::create(this, name_arg, sph)))) + if (unlikely(!(pkg= sp_package::create(this, name_arg, sph, + sp_mem_root_ptr)))) return NULL; pkg->reset_thd_mem_root(thd); pkg->init(this);