Commit bd7c3162 authored by Michael Widenius's avatar Michael Widenius

MDEV-17065 Crash on SHOW CREATE TABLE with CHECK CONSTRAINT

The problem was that the original alias was replaced with a new allocated
string, but constraint item's are still pointing to the original alias.

Fixed by storing the original alias used when printing constraint in the
tables mem_root.
parent 4dc20ff6
......@@ -74,3 +74,40 @@ CREATE TABLE t_illegal (col_1 INT CHECK something (whatever));
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'something (whatever))' at line 1
CREATE TABLE t_illegal (col_1 INT CHECK something);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'something)' at line 1
CREATE TABLE long_enough_name (
pk int(11) NOT NULL,
f1 int(11) DEFAULT NULL,
f2 int(11) NOT NULL,
f3 int(11) DEFAULT NULL,
f4 timestamp NOT NULL DEFAULT current_timestamp(),
f5 varchar(32) COLLATE utf8_bin NOT NULL DEFAULT 'foo',
f6 smallint(6) NOT NULL DEFAULT 1,
f7 int(11) DEFAULT NULL,
PRIMARY KEY (pk),
KEY idx1 (f7),
KEY idx2 (f1),
KEY idx3 (f2),
KEY idx4 (f3),
CONSTRAINT constr CHECK (f6 >= 0)
);
SELECT * FROM long_enough_name AS tbl;
pk f1 f2 f3 f4 f5 f6 f7
SHOW CREATE TABLE long_enough_name;
Table Create Table
long_enough_name CREATE TABLE `long_enough_name` (
`pk` int(11) NOT NULL,
`f1` int(11) DEFAULT NULL,
`f2` int(11) NOT NULL,
`f3` int(11) DEFAULT NULL,
`f4` timestamp NOT NULL DEFAULT current_timestamp(),
`f5` varchar(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT 'foo',
`f6` smallint(6) NOT NULL DEFAULT 1,
`f7` int(11) DEFAULT NULL,
PRIMARY KEY (`pk`),
KEY `idx1` (`f7`),
KEY `idx2` (`f1`),
KEY `idx3` (`f2`),
KEY `idx4` (`f3`),
CONSTRAINT `constr` CHECK (`f6` >= 0)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE long_enough_name;
......@@ -77,3 +77,28 @@ CREATE TABLE t_illegal (col_1 INT CHECK something (whatever));
--error ER_PARSE_ERROR
CREATE TABLE t_illegal (col_1 INT CHECK something);
#
# MDEV-17065 Crash on SHOW CREATE TABLE with CHECK CONSTRAINT
#
CREATE TABLE long_enough_name (
pk int(11) NOT NULL,
f1 int(11) DEFAULT NULL,
f2 int(11) NOT NULL,
f3 int(11) DEFAULT NULL,
f4 timestamp NOT NULL DEFAULT current_timestamp(),
f5 varchar(32) COLLATE utf8_bin NOT NULL DEFAULT 'foo',
f6 smallint(6) NOT NULL DEFAULT 1,
f7 int(11) DEFAULT NULL,
PRIMARY KEY (pk),
KEY idx1 (f7),
KEY idx2 (f1),
KEY idx3 (f2),
KEY idx4 (f3),
CONSTRAINT constr CHECK (f6 >= 0)
);
SELECT * FROM long_enough_name AS tbl;
SHOW CREATE TABLE long_enough_name;
DROP TABLE long_enough_name;
......@@ -3007,6 +3007,8 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
{
enum open_frm_error error;
uint records, i, bitmap_size, bitmap_count;
size_t tmp_length;
const char *tmp_alias;
bool error_reported= FALSE;
uchar *record, *bitmaps;
Field **field_ptr;
......@@ -3033,8 +3035,15 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
}
init_sql_alloc(&outparam->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0, MYF(0));
if (outparam->alias.copy(alias, strlen(alias), table_alias_charset))
/*
We have to store the original alias in mem_root as constraints and virtual
functions may store pointers to it
*/
tmp_length= strlen(alias);
if (!(tmp_alias= strmake_root(&outparam->mem_root, alias, tmp_length)))
goto err;
outparam->alias.set(tmp_alias, tmp_length, table_alias_charset);
outparam->quick_keys.init();
outparam->covering_keys.init();
outparam->intersect_keys.init();
......@@ -4487,7 +4496,7 @@ void TABLE::init(THD *thd, TABLE_LIST *tl)
s->table_name.str,
tl->alias);
/* Fix alias if table name changes. */
if (strcmp(alias.c_ptr(), tl->alias))
if (!alias.alloced_length() || strcmp(alias.c_ptr(), tl->alias))
alias.copy(tl->alias, strlen(tl->alias), alias.charset());
tablenr= thd->current_tablenr++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment