mirror of
https://github.com/MariaDB/server.git
synced 2025-01-17 04:22:27 +01:00
7f0f18cd6e
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.
131 lines
3 KiB
Text
131 lines
3 KiB
Text
#
|
|
# Bug #47412: Valgrind warnings / user can read uninitalized memory
|
|
# using SP variables
|
|
#
|
|
CREATE SCHEMA testdb;
|
|
USE testdb;
|
|
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|
|
|
# should not return valgrind warnings
|
|
CALL p3 ( f2 () );
|
|
ERROR 42000: PROCEDURE testdb.p_not_exists does not exist
|
|
DROP SCHEMA testdb;
|
|
CREATE SCHEMA testdb;
|
|
USE testdb;
|
|
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|
|
|
# should not return valgrind warnings
|
|
CALL p3 ( f2 () );
|
|
ERROR 42000: PROCEDURE testdb.p_not_exists does not exist
|
|
DROP SCHEMA testdb;
|
|
CREATE SCHEMA testdb;
|
|
USE testdb;
|
|
CREATE FUNCTION f2 () RETURNS INTEGER
|
|
BEGIN
|
|
DECLARE CONTINUE HANDLER FOR SQLSTATE '42000' SET @aux = 1;
|
|
RETURN f_not_exists () ;
|
|
END|
|
|
# should not return valgrind warnings
|
|
SELECT f2 ();
|
|
f2 ()
|
|
NULL
|
|
DROP SCHEMA testdb;
|
|
USE test;
|
|
#
|
|
# Bug#50423: Crash on second call of a procedure dropping a trigger
|
|
#
|
|
DROP TABLE IF EXISTS t1;
|
|
DROP TRIGGER IF EXISTS tr1;
|
|
DROP PROCEDURE IF EXISTS p1;
|
|
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 ();
|
|
CALL p1 ();
|
|
ERROR HY000: Trigger does not exist
|
|
DROP TABLE t1;
|
|
DROP PROCEDURE p1;
|
|
#
|
|
# Bug#50423: Crash on second call of a procedure dropping a trigger
|
|
#
|
|
DROP TABLE IF EXISTS t1;
|
|
DROP TRIGGER IF EXISTS tr1;
|
|
DROP PROCEDURE IF EXISTS p1;
|
|
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 ();
|
|
CALL p1 ();
|
|
ERROR HY000: Trigger does not exist
|
|
DROP TABLE t1;
|
|
DROP PROCEDURE p1;
|
|
#
|
|
# Bug#54375: Error in stored procedure leaves connection
|
|
# in different default schema
|
|
#
|
|
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);
|
|
CREATE FUNCTION f1 (
|
|
some_value int
|
|
)
|
|
RETURNS smallint
|
|
DETERMINISTIC
|
|
BEGIN
|
|
INSERT INTO t1 SET c1 = some_value;
|
|
RETURN(LAST_INSERT_ID());
|
|
END$$
|
|
DROP DATABASE IF EXISTS db2;
|
|
CREATE DATABASE db2;
|
|
USE db2;
|
|
SELECT DATABASE();
|
|
DATABASE()
|
|
db2
|
|
SELECT db1.f1(1);
|
|
ERROR 23000: Duplicate entry '1' for key 'PRIMARY'
|
|
SELECT DATABASE();
|
|
DATABASE()
|
|
db2
|
|
USE test;
|
|
DROP FUNCTION db1.f1;
|
|
DROP TABLE db1.t1;
|
|
DROP DATABASE db1;
|
|
DROP DATABASE db2;
|
|
#
|
|
# Bug#13105873:valgrind warning:possible crash in foreign
|
|
# key handling on subsequent create table if not exists
|
|
#
|
|
DROP DATABASE IF EXISTS testdb;
|
|
CREATE DATABASE testdb;
|
|
USE testdb;
|
|
CREATE TABLE t1 (id1 INT PRIMARY KEY);
|
|
CREATE PROCEDURE `p1`()
|
|
BEGIN
|
|
CREATE TABLE IF NOT EXISTS t2(id INT PRIMARY KEY,
|
|
CONSTRAINT FK FOREIGN KEY (id) REFERENCES t1( id1 ));
|
|
END$
|
|
CALL p1();
|
|
# below stmt should not return valgrind warnings
|
|
CALL p1();
|
|
Warnings:
|
|
Note 1050 Table 't2' already exists
|
|
DROP DATABASE testdb;
|
|
End of 5.1 tests
|