mirror of
https://github.com/MariaDB/server.git
synced 2025-11-06 13:10:12 +03:00
KEY HANDLING ON SUBSEQUENT CREATE TABLE IF NOT EXISTS
PROBLEM:
--------
Consider a SP routine which does CREATE TABLE
with REFERENCES clause. The first call to this routine
invokes parser and the parsed items are cached, so as
to avoid parsing for the second execution of the routine.
It is obsevered that valgrind reports a warning
upon read of thd->lex->alter_info->key_list->Foreign_key object,
which seem to be pointing to a invalid memory address
during second time execution of the routine. Accessing this object
theoretically could cause a crash.
ANALYSIS:
---------
The problem stems from the fact that for some reason
elements of ref_columns list in thd->lex->alter_info->
key_list->Foreign_key object are changed to point to
objects allocated on runtime memory root.
During the first execution of routine we create
a copy of thd->lex->alter_info object.
As part of this process we create a clones of objects in
Alter_info::key_list and of Foreign_key object in particular.
Then Foreign_key object is cloned for some reason we
perform shallow copies of both Foreign_key::ref_columns
and Foreign_key::columns list. So new instance of
Foreign_key object starts to SHARE contents of ref_columns
and columns list with the original instance.
After that as part of cloning process we call
list_copy_and_replace_each_value() for elements of
ref_columns list. As result ref_columns lists in both
original and cloned Foreign_key object start to contain
pointers to Key_part_spec objects allocated on runtime
memory root because of shallow copy.
So when we start copying of thd->lex->alter_info object
during the second execution of stored routine we indeed
encounter pointer to the Key_part_spec object allocated
on runtime mem-root which was cleared during at the end
of previous execution. This is done in sp_head::execute(),
by a call to free_root(&execute_mem_root,MYF(0));
As result we get valgrind warnings about accessing
unreferenced memory.
FIX:
----
The safest solution to this problem is to
fix Foreign_key(Foreign_key, MEM_ROOT) constructor to do
a deep copy of columns lists, similar to Key(Key, MEM_ROOT)
constructor.
166 lines
3.4 KiB
Plaintext
166 lines
3.4 KiB
Plaintext
# Test file for stored procedure bugfixes
|
|
|
|
--echo #
|
|
--echo # Bug #47412: Valgrind warnings / user can read uninitalized memory
|
|
--echo # using SP variables
|
|
--echo #
|
|
|
|
CREATE SCHEMA testdb;
|
|
USE testdb;
|
|
DELIMITER |;
|
|
CREATE FUNCTION f2 () RETURNS INTEGER
|
|
BEGIN
|
|
DECLARE CONTINUE HANDLER FOR SQLSTATE '42000' SET @aux = 1;
|
|
RETURN f_not_exists () ;
|
|
END|
|
|
CREATE PROCEDURE p3 ( arg1 VARCHAR(32) )
|
|
BEGIN
|
|
CALL p_not_exists ( );
|
|
END|
|
|
DELIMITER ;|
|
|
--echo # should not return valgrind warnings
|
|
--error ER_SP_DOES_NOT_EXIST
|
|
CALL p3 ( f2 () );
|
|
|
|
DROP SCHEMA testdb;
|
|
|
|
CREATE SCHEMA testdb;
|
|
USE testdb;
|
|
DELIMITER |;
|
|
CREATE FUNCTION f2 () RETURNS INTEGER
|
|
BEGIN
|
|
DECLARE CONTINUE HANDLER FOR SQLSTATE '42000' SET @aux = 1;
|
|
RETURN f_not_exists () ;
|
|
END|
|
|
CREATE PROCEDURE p3 ( arg2 INTEGER )
|
|
BEGIN
|
|
CALL p_not_exists ( );
|
|
END|
|
|
DELIMITER ;|
|
|
--echo # should not return valgrind warnings
|
|
--error ER_SP_DOES_NOT_EXIST
|
|
CALL p3 ( f2 () );
|
|
|
|
DROP SCHEMA testdb;
|
|
|
|
CREATE SCHEMA testdb;
|
|
USE testdb;
|
|
DELIMITER |;
|
|
CREATE FUNCTION f2 () RETURNS INTEGER
|
|
BEGIN
|
|
DECLARE CONTINUE HANDLER FOR SQLSTATE '42000' SET @aux = 1;
|
|
RETURN f_not_exists () ;
|
|
END|
|
|
DELIMITER ;|
|
|
--echo # should not return valgrind warnings
|
|
SELECT f2 ();
|
|
|
|
DROP SCHEMA testdb;
|
|
|
|
USE test;
|
|
|
|
--echo #
|
|
--echo # Bug#50423: Crash on second call of a procedure dropping a trigger
|
|
--echo #
|
|
|
|
--disable_warnings
|
|
DROP TABLE IF EXISTS t1;
|
|
DROP TRIGGER IF EXISTS tr1;
|
|
DROP PROCEDURE IF EXISTS p1;
|
|
--enable_warnings
|
|
|
|
CREATE TABLE t1 (f1 INTEGER);
|
|
CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW SET @aux = 1;
|
|
CREATE PROCEDURE p1 () DROP TRIGGER tr1;
|
|
|
|
CALL p1 ();
|
|
--error ER_TRG_DOES_NOT_EXIST
|
|
CALL p1 ();
|
|
|
|
DROP TABLE t1;
|
|
DROP PROCEDURE p1;
|
|
|
|
--echo #
|
|
--echo # Bug#50423: Crash on second call of a procedure dropping a trigger
|
|
--echo #
|
|
|
|
--disable_warnings
|
|
DROP TABLE IF EXISTS t1;
|
|
DROP TRIGGER IF EXISTS tr1;
|
|
DROP PROCEDURE IF EXISTS p1;
|
|
--enable_warnings
|
|
|
|
CREATE TABLE t1 (f1 INTEGER);
|
|
CREATE TRIGGER tr1 BEFORE INSERT ON t1 FOR EACH ROW SET @aux = 1;
|
|
CREATE PROCEDURE p1 () DROP TRIGGER tr1;
|
|
|
|
CALL p1 ();
|
|
--error ER_TRG_DOES_NOT_EXIST
|
|
CALL p1 ();
|
|
|
|
DROP TABLE t1;
|
|
DROP PROCEDURE p1;
|
|
|
|
--echo #
|
|
--echo # Bug#54375: Error in stored procedure leaves connection
|
|
--echo # in different default schema
|
|
--echo #
|
|
|
|
--disable_warnings
|
|
SET @@SQL_MODE = 'STRICT_ALL_TABLES';
|
|
DROP DATABASE IF EXISTS db1;
|
|
CREATE DATABASE db1;
|
|
USE db1;
|
|
DROP TABLE IF EXISTS t1;
|
|
CREATE TABLE t1 (c1 int NOT NULL PRIMARY KEY);
|
|
INSERT INTO t1 VALUES (1);
|
|
DELIMITER $$;
|
|
CREATE FUNCTION f1 (
|
|
some_value int
|
|
)
|
|
RETURNS smallint
|
|
DETERMINISTIC
|
|
BEGIN
|
|
INSERT INTO t1 SET c1 = some_value;
|
|
RETURN(LAST_INSERT_ID());
|
|
END$$
|
|
DELIMITER ;$$
|
|
DROP DATABASE IF EXISTS db2;
|
|
CREATE DATABASE db2;
|
|
--enable_warnings
|
|
USE db2;
|
|
SELECT DATABASE();
|
|
--error ER_DUP_ENTRY
|
|
SELECT db1.f1(1);
|
|
SELECT DATABASE();
|
|
USE test;
|
|
DROP FUNCTION db1.f1;
|
|
DROP TABLE db1.t1;
|
|
DROP DATABASE db1;
|
|
DROP DATABASE db2;
|
|
|
|
--echo #
|
|
--echo # Bug#13105873:valgrind warning:possible crash in foreign
|
|
--echo # key handling on subsequent create table if not exists
|
|
--echo #
|
|
|
|
--disable_warnings
|
|
DROP DATABASE IF EXISTS testdb;
|
|
--enable_warnings
|
|
CREATE DATABASE testdb;
|
|
USE testdb;
|
|
CREATE TABLE t1 (id1 INT PRIMARY KEY);
|
|
DELIMITER $;
|
|
CREATE PROCEDURE `p1`()
|
|
BEGIN
|
|
CREATE TABLE IF NOT EXISTS t2(id INT PRIMARY KEY,
|
|
CONSTRAINT FK FOREIGN KEY (id) REFERENCES t1( id1 ));
|
|
END$
|
|
DELIMITER ;$
|
|
CALL p1();
|
|
--echo # below stmt should not return valgrind warnings
|
|
CALL p1();
|
|
DROP DATABASE testdb;
|
|
|
|
--echo End of 5.1 tests
|