Commit ff2d92b1 authored by Alexander Barkov's avatar Alexander Barkov

MDEV-7231 Field ROUTINE_DEFINITION in INFORMATION_SCHEMA.`ROUTINES`

contains broken procedure body when used shielding quotes inside.
parent 28a36f61
......@@ -533,6 +533,7 @@ struct my_charset_handler_st
extern MY_CHARSET_HANDLER my_charset_8bit_handler;
extern MY_CHARSET_HANDLER my_charset_ucs2_handler;
extern MY_CHARSET_HANDLER my_charset_utf8_handler;
/*
......@@ -889,6 +890,18 @@ uint32 my_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
const char *from, uint32 from_length,
CHARSET_INFO *from_cs, uint *errors);
/**
An extended version of my_convert(), to pass non-default mb_wc() and wc_mb().
For example, String::copy_printable() which is used in
Protocol::store_warning() uses this to escape control
and non-convertable characters.
*/
uint32 my_convert_using_func(char *to, uint32 to_length, CHARSET_INFO *to_cs,
my_charset_conv_wc_mb mb_wc,
const char *from, uint32 from_length,
CHARSET_INFO *from_cs,
my_charset_conv_mb_wc wc_mb,
uint *errors);
/*
Convert a string between two character sets.
Bad byte sequences as well as characters that cannot be
......
......@@ -10259,5 +10259,146 @@ Warnings:
Note 1003 select `test`.`t1`.`c` AS `c` from `test`.`t1` where (`test`.`t1`.`c` = 'A')
DROP TABLE t1;
#
# MDEV-7231 Field ROUTINE_DEFINITION in INFORMATION_SCHEMA.`ROUTINES` contains broken procedure body when used shielding quotes inside.
#
CREATE PROCEDURE p1()
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """", '\'', "\"";
SELECT '<tab> <tab>\t<tab>';
SELECT '<nl>
<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT _binary'test';
SELECT _binary'test\0';
SELECT N'''', N"""", N'\'', N"\"";
SELECT N'<tab> <tab>\t<tab>';
SELECT N'<nl>
<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END$$
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='p1';
ROUTINE_DEFINITION
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """", '''', """";
SELECT '<tab>\t<tab>\t<tab>';
SELECT '<nl>\n<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT 'test';
SELECT 'test\0';
SELECT N'''', N"""", N'''', N"""";
SELECT N'<tab>\t<tab>\t<tab>';
SELECT N'<nl>\n<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END
SELECT body_utf8 FROM mysql.proc WHERE name='p1';
body_utf8
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """", '''', """";
SELECT '<tab>\t<tab>\t<tab>';
SELECT '<nl>\n<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT 'test';
SELECT 'test\0';
SELECT N'''', N"""", N'''', N"""";
SELECT N'<tab>\t<tab>\t<tab>';
SELECT N'<nl>\n<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END
DROP PROCEDURE p1;
SET @@SQL_MODE='NO_BACKSLASH_ESCAPES';
CREATE PROCEDURE p1()
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """";
SELECT '<tab> <tab>\t<tab>';
SELECT '<nl>
<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT _binary'test';
SELECT _binary'test\0';
SELECT N'''', N"""";
SELECT N'<tab> <tab>\t<tab>';
SELECT N'<nl>
<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END$$
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='p1';
ROUTINE_DEFINITION
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """";
SELECT '<tab> <tab>\t<tab>';
SELECT '<nl>
<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT 'test';
SELECT 'test\0';
SELECT N'''', N"""";
SELECT N'<tab> <tab>\t<tab>';
SELECT N'<nl>
<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END
SELECT body_utf8 FROM mysql.proc WHERE name='p1';
body_utf8
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """";
SELECT '<tab> <tab>\t<tab>';
SELECT '<nl>
<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT 'test';
SELECT 'test\0';
SELECT N'''', N"""";
SELECT N'<tab> <tab>\t<tab>';
SELECT N'<nl>
<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END
DROP PROCEDURE p1;
SET @@SQL_MODE=default;
#
# End of 10.1 tests
#
......@@ -3382,5 +3382,19 @@ SET NAMES utf8mb4;
SELECT * FROM `test😁😁test`;
ERROR HY000: Invalid utf8mb4 character string: 'test\xF0\x9F\x98\x81\xF0\x9F\x98\x81test'
#
# MDEV-7231 Field ROUTINE_DEFINITION in INFORMATION_SCHEMA.`ROUTINES` contains broken procedure body when used shielding quotes inside.
#
SET NAMES utf8mb4;
CREATE FUNCTION f1() RETURNS TEXT CHARACTER SET utf8mb4
RETURN CONCAT('😎','x😎','😎y','x😎y');
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='f1';
ROUTINE_DEFINITION
RETURN CONCAT('?','x?','?y','x?y')
SELECT body_utf8 FROM mysql.proc WHERE name='f1';
body_utf8
RETURN CONCAT('?','x?','?y','x?y')
DROP FUNCTION f1;
#
# End of 10.1 tests
#
......@@ -1871,6 +1871,82 @@ SELECT * FROM t1 WHERE c>=_utf8'a' COLLATE utf8_general_ci AND c='A';
DROP TABLE t1;
--echo #
--echo # MDEV-7231 Field ROUTINE_DEFINITION in INFORMATION_SCHEMA.`ROUTINES` contains broken procedure body when used shielding quotes inside.
--echo #
DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """", '\'', "\"";
SELECT '<tab> <tab>\t<tab>';
SELECT '<nl>
<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT _binary'test';
SELECT _binary'test\0';
SELECT N'''', N"""", N'\'', N"\"";
SELECT N'<tab> <tab>\t<tab>';
SELECT N'<nl>
<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END$$
DELIMITER ;$$
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='p1';
SELECT body_utf8 FROM mysql.proc WHERE name='p1';
DROP PROCEDURE p1;
SET @@SQL_MODE='NO_BACKSLASH_ESCAPES';
DELIMITER $$;
CREATE PROCEDURE p1()
BEGIN
SELECT CONCAT('ABC = ''',1,''''), CONCAT('ABC = ',2);
SELECT '''', """";
SELECT '<tab> <tab>\t<tab>';
SELECT '<nl>
<nl>\n<nl>';
SELECT 'test';
SELECT 'tëst';
SELECT 'test\0';
SELECT 'tëst\0';
SELECT _binary'test';
SELECT _binary'test\0';
SELECT N'''', N"""";
SELECT N'<tab> <tab>\t<tab>';
SELECT N'<nl>
<nl>\n<nl>';
SELECT N'test';
SELECT N'tëst';
SELECT N'test\0';
SELECT N'tëst\0';
END$$
DELIMITER ;$$
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='p1';
SELECT body_utf8 FROM mysql.proc WHERE name='p1';
DROP PROCEDURE p1;
SET @@SQL_MODE=default;
# TODO: Uncomment the below test whe we fix:
# MDEV-9623INFORMATION_SCHEMA.ROUTINES.ROUTINE_DEFINITION does not handle binary literals well
#
#SET NAMES binary;
#CREATE FUNCTION f1() RETURNS TEXT RETURN CONCAT('i','й');
#SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
#WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='f1';
#SELECT body_utf8 FROM mysql.proc WHERE name='f1';
#DROP FUNCTION f1;
#SET NAMES utf8;
--echo #
--echo # End of 10.1 tests
--echo #
......@@ -1904,6 +1904,18 @@ SET NAMES utf8mb4;
--error ER_INVALID_CHARACTER_STRING
SELECT * FROM `test😁😁test`;
--echo #
--echo # MDEV-7231 Field ROUTINE_DEFINITION in INFORMATION_SCHEMA.`ROUTINES` contains broken procedure body when used shielding quotes inside.
--echo #
# Non-BMP characters should be replaced to '?' in ROUTINE_DEFINITION/body_utf8
SET NAMES utf8mb4;
CREATE FUNCTION f1() RETURNS TEXT CHARACTER SET utf8mb4
RETURN CONCAT('😎','x😎','😎y','x😎y');
SELECT ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_SCHEMA='test' AND SPECIFIC_NAME ='f1';
SELECT body_utf8 FROM mysql.proc WHERE name='f1';
DROP FUNCTION f1;
--echo #
--echo # End of 10.1 tests
--echo #
This diff is collapsed.
......@@ -1807,6 +1807,7 @@ class Lex_input_stream
{
size_t unescape(CHARSET_INFO *cs, char *to,
const char *str, const char *end, int sep);
my_charset_conv_wc_mb get_escape_func(THD *thd, my_wc_t sep) const;
public:
Lex_input_stream()
{
......@@ -2077,14 +2078,23 @@ public:
return (uint) (m_body_utf8_ptr - m_body_utf8);
}
/**
Get the maximum length of the utf8-body buffer.
The utf8 body can grow because of the character set conversion and escaping.
*/
uint get_body_utf8_maximum_length(THD *thd);
void body_utf8_start(THD *thd, const char *begin_ptr);
void body_utf8_append(const char *ptr);
void body_utf8_append(const char *ptr, const char *end_ptr);
void body_utf8_append_literal(THD *thd,
const LEX_STRING *txt,
CHARSET_INFO *txt_cs,
const char *end_ptr);
void body_utf8_append_ident(THD *thd,
const LEX_STRING *txt,
const char *end_ptr);
void body_utf8_append_escape(THD *thd,
const LEX_STRING *txt,
CHARSET_INFO *txt_cs,
const char *end_ptr,
my_wc_t sep);
/** Current thread. */
THD *m_thd;
......@@ -2105,7 +2115,7 @@ public:
/** LALR(2) resolution, value of the look ahead token.*/
LEX_YYSTYPE lookahead_yylval;
bool get_text(LEX_STRING *to, int pre_skip, int post_skip);
bool get_text(LEX_STRING *to, uint sep, int pre_skip, int post_skip);
void add_digest_token(uint token, LEX_YYSTYPE yylval);
......
......@@ -1030,19 +1030,18 @@ my_charset_is_ascii_compatible(CHARSET_INFO *cs)
@return Number of bytes copied to 'to' string
*/
static uint32
my_convert_internal(char *to, uint32 to_length,
CHARSET_INFO *to_cs,
const char *from, uint32 from_length,
CHARSET_INFO *from_cs, uint *errors)
uint32
my_convert_using_func(char *to, uint32 to_length,
CHARSET_INFO *to_cs, my_charset_conv_wc_mb wc_mb,
const char *from, uint32 from_length,
CHARSET_INFO *from_cs, my_charset_conv_mb_wc mb_wc,
uint *errors)
{
int cnvres;
my_wc_t wc;
const uchar *from_end= (const uchar*) from + from_length;
char *to_start= to;
uchar *to_end= (uchar*) to + to_length;
my_charset_conv_mb_wc mb_wc= from_cs->cset->mb_wc;
my_charset_conv_wc_mb wc_mb= to_cs->cset->wc_mb;
uint error_count= 0;
while (1)
......@@ -1119,8 +1118,11 @@ my_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
immediately switch to slow mb_wc->wc_mb method.
*/
if ((to_cs->state | from_cs->state) & MY_CS_NONASCII)
return my_convert_internal(to, to_length, to_cs,
from, from_length, from_cs, errors);
return my_convert_using_func(to, to_length,
to_cs, to_cs->cset->wc_mb,
from, from_length,
from_cs, from_cs->cset->mb_wc,
errors);
length= length2= MY_MIN(to_length, from_length);
......@@ -1152,9 +1154,11 @@ my_convert(char *to, uint32 to_length, CHARSET_INFO *to_cs,
uint32 copied_length= length2 - length;
to_length-= copied_length;
from_length-= copied_length;
return copied_length + my_convert_internal(to, to_length, to_cs,
from, from_length, from_cs,
errors);
return copied_length + my_convert_using_func(to, to_length, to_cs,
to_cs->cset->wc_mb,
from, from_length, from_cs,
from_cs->cset->mb_wc,
errors);
}
}
......
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