Commit 71affc14 authored by Jon Olav Hauglid's avatar Jon Olav Hauglid

Bug #56678 Valgrind warnings from binlog.binlog_unsafe

After the patch for Bug#54579, multi inserts done with INSERT DELAYED
are binlogged as normal INSERT. During processing of the statement,
a new query string without the DELAYED keyword is made. The problem
was that this new string was incorrectly made when the INSERT DELAYED
was part of a prepared statement - data was read outside the allocated
buffer.

The reason for this bug was that a pointer to the position of the
DELAYED keyword inside the query string was stored when parsing the
statement. This pointer was then later (at runtime) used (via pointer
subtraction) to find the number of characters to skip when making a
new query string without DELAYED. But when the statement was re-executed
as part of a prepared statement, the original pointer would be invalid
and the pointer subtraction would give a wrong/random result.

This patch fixes the problem by instead storing the offsets from the
beginning of the query string to the start and end of the DELAYED 
keyword. These values will not depend on the memory position
of the query string at runtime and therefore not give wrong results
when the statement is executed in a prepared statement.

This bug was a regression introduced by the patch for Bug#54579.

No test case added as this bug is already covered by the existing
binlog.binlog_unsafe test case when running with valgrind.
parent 11c4d8ba
...@@ -634,14 +634,12 @@ bool open_and_lock_for_insert_delayed(THD *thd, TABLE_LIST *table_list) ...@@ -634,14 +634,12 @@ bool open_and_lock_for_insert_delayed(THD *thd, TABLE_LIST *table_list)
static int static int
create_insert_stmt_from_insert_delayed(THD *thd, String *buf) create_insert_stmt_from_insert_delayed(THD *thd, String *buf)
{ {
/* Append the part of thd->query before "DELAYED" keyword */ /* Make a copy of thd->query() and then remove the "DELAYED" keyword */
if (buf->append(thd->query(), if (buf->append(thd->query()) ||
thd->lex->keyword_delayed_begin - thd->query())) buf->replace(thd->lex->keyword_delayed_begin_offset,
thd->lex->keyword_delayed_end_offset -
thd->lex->keyword_delayed_begin_offset, 0))
return 1; return 1;
/* Append the part of thd->query after "DELAYED" keyword */
if (buf->append(thd->lex->keyword_delayed_begin + 7))
return 1;
return 0; return 0;
} }
......
...@@ -2355,15 +2355,19 @@ struct LEX: public Query_tables_list ...@@ -2355,15 +2355,19 @@ struct LEX: public Query_tables_list
This pointer is required to add possibly omitted DEFINER-clause to the This pointer is required to add possibly omitted DEFINER-clause to the
DDL-statement before dumping it to the binlog. DDL-statement before dumping it to the binlog.
keyword_delayed_begin points to the begin of the DELAYED keyword in keyword_delayed_begin_offset is the offset to the beginning of the DELAYED
INSERT DELAYED statement. keyword in INSERT DELAYED statement. keyword_delayed_end_offset is the
offset to the character right after the DELAYED keyword.
*/ */
union { union {
const char *stmt_definition_begin; const char *stmt_definition_begin;
const char *keyword_delayed_begin; uint keyword_delayed_begin_offset;
}; };
union {
const char *stmt_definition_end; const char *stmt_definition_end;
uint keyword_delayed_end_offset;
};
/** /**
During name resolution search only in the table list given by During name resolution search only in the table list given by
......
...@@ -10447,7 +10447,10 @@ insert_lock_option: ...@@ -10447,7 +10447,10 @@ insert_lock_option:
| LOW_PRIORITY { $$= TL_WRITE_LOW_PRIORITY; } | LOW_PRIORITY { $$= TL_WRITE_LOW_PRIORITY; }
| DELAYED_SYM | DELAYED_SYM
{ {
Lex->keyword_delayed_begin= YYLIP->get_tok_start(); Lex->keyword_delayed_begin_offset= (uint)(YYLIP->get_tok_start() -
YYTHD->query());
Lex->keyword_delayed_end_offset= Lex->keyword_delayed_begin_offset +
YYLIP->yyLength() + 1;
$$= TL_WRITE_DELAYED; $$= TL_WRITE_DELAYED;
} }
| HIGH_PRIORITY { $$= TL_WRITE; } | HIGH_PRIORITY { $$= TL_WRITE; }
...@@ -10457,7 +10460,10 @@ replace_lock_option: ...@@ -10457,7 +10460,10 @@ replace_lock_option:
opt_low_priority { $$= $1; } opt_low_priority { $$= $1; }
| DELAYED_SYM | DELAYED_SYM
{ {
Lex->keyword_delayed_begin= YYLIP->get_tok_start(); Lex->keyword_delayed_begin_offset= (uint)(YYLIP->get_tok_start() -
YYTHD->query());
Lex->keyword_delayed_end_offset= Lex->keyword_delayed_begin_offset +
YYLIP->yyLength() + 1;
$$= TL_WRITE_DELAYED; $$= TL_WRITE_DELAYED;
} }
; ;
......
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