Skip to content

Commit

Permalink
MDEV-35510 ASAN build crashes during bootstrap
Browse files Browse the repository at this point in the history
Avoid ASAN failure by collecting statistics from Result objects
before cleaning them up.  In related single-table cases, statistics
are maintained directly by the single-table update and delete
functions.
  • Loading branch information
DaveGosselin-MariaDB committed Feb 4, 2025
1 parent 6be0940 commit 8505dc7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
7 changes: 7 additions & 0 deletions sql/sql_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#ifndef SQL_CMD_INCLUDED
#define SQL_CMD_INCLUDED

#include <my_base.h>

/*
When a command is added here, be sure it's also added in mysqld.cc
in "struct show_var_st status_vars[]= {" ...
Expand Down Expand Up @@ -224,6 +226,11 @@ class Sql_cmd : public Sql_alloc
*/
virtual bool is_dml() const { return false; }

virtual void get_dml_stat (ha_rows &found, ha_rows &changed)
{
found= changed= 0;
}

/**
@brief Unprepare prepared statement for the command
@param thd global context of the processed statement
Expand Down
4 changes: 3 additions & 1 deletion sql/sql_delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ bool Sql_cmd_delete::delete_from_single_table(THD *thd)
SQL_SELECT *select= 0;
SORT_INFO *file_sort= 0;
READ_RECORD info;
ha_rows deleted= 0;
bool reverse= FALSE;
bool binlog_is_row;
killed_state killed_status= NOT_KILLED;
Expand Down Expand Up @@ -1861,6 +1860,9 @@ bool Sql_cmd_delete::execute_inner(THD *thd)

if (result)
{
/* In single table case, this->deleted set by delete_from_single_table */
if (res && multitable)
deleted= ((multi_delete*)get_result())->num_deleted();
res= false;
delete result;
}
Expand Down
7 changes: 7 additions & 0 deletions sql/sql_delete.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ template <typename T> class SQL_I_List;
class Sql_cmd_delete final : public Sql_cmd_dml
{
public:
ha_rows deleted{0};
Sql_cmd_delete(bool multitable_arg)
: orig_multitable(multitable_arg), multitable(multitable_arg),
save_protocol(NULL)
Expand All @@ -66,6 +67,12 @@ class Sql_cmd_delete final : public Sql_cmd_dml

void remove_order_by_without_limit(THD *thd);

void get_dml_stat (ha_rows &found, ha_rows &changed) override
{
found= 0;
changed= deleted;
}

protected:
/**
@brief Perform precheck of table privileges for delete statements
Expand Down
52 changes: 27 additions & 25 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34139,39 +34139,38 @@ static void MYSQL_DML_START(THD *thd)
}


static void MYSQL_DML_DONE(THD *thd, int rc)
static void MYSQL_DML_GET_STAT(THD * thd, ha_rows &found, ha_rows &changed)
{
switch (thd->lex->sql_command) {
case SQLCOM_UPDATE:
case SQLCOM_UPDATE_MULTI:
case SQLCOM_DELETE_MULTI:
thd->lex->m_sql_cmd->get_dml_stat(found, changed);
break;
case SQLCOM_DELETE:
found= 0;
changed= (thd->get_row_count_func());
break;
default:
DBUG_ASSERT(0);
}
}


static void MYSQL_DML_DONE(THD *thd, int rc, ha_rows found, ha_rows changed)
{
switch (thd->lex->sql_command) {
case SQLCOM_UPDATE:
MYSQL_UPDATE_DONE(
rc,
(rc ? 0 :
((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result()))
->num_found()),
(rc ? 0 :
((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result()))
->num_updated()));
MYSQL_UPDATE_DONE(rc, found, changed);
break;
case SQLCOM_UPDATE_MULTI:
MYSQL_MULTI_UPDATE_DONE(
rc,
(rc ? 0 :
((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result()))
->num_found()),
(rc ? 0 :
((multi_update*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result()))
->num_updated()));
MYSQL_MULTI_UPDATE_DONE(rc, found, changed);
break;
case SQLCOM_DELETE:
MYSQL_DELETE_DONE(rc, (rc ? 0 : (ulong) (thd->get_row_count_func())));
MYSQL_DELETE_DONE(rc, changed);
break;
case SQLCOM_DELETE_MULTI:
MYSQL_MULTI_DELETE_DONE(
rc,
(rc ? 0 :
((multi_delete*)(((Sql_cmd_dml*)(thd->lex->m_sql_cmd))->get_result()))
->num_deleted()));
MYSQL_MULTI_DELETE_DONE(rc, changed);
break;
default:
DBUG_ASSERT(0);
Expand Down Expand Up @@ -34260,6 +34259,7 @@ bool Sql_cmd_dml::prepare(THD *thd)
bool Sql_cmd_dml::execute(THD *thd)
{
lex = thd->lex;
ha_rows found= 0, changed= 0;
bool res;

SELECT_LEX_UNIT *unit = &lex->unit;
Expand Down Expand Up @@ -34310,6 +34310,8 @@ bool Sql_cmd_dml::execute(THD *thd)

if (res)
goto err;
else
MYSQL_DML_GET_STAT(thd, found, changed);

res= unit->cleanup();

Expand All @@ -34318,13 +34320,13 @@ bool Sql_cmd_dml::execute(THD *thd)

THD_STAGE_INFO(thd, stage_end);

MYSQL_DML_DONE(thd, res);
MYSQL_DML_DONE(thd, 0, found, changed);

return res;

err:
DBUG_ASSERT(thd->is_error() || thd->killed);
MYSQL_DML_DONE(thd, 1);
MYSQL_DML_DONE(thd, 1, 0, 0);
THD_STAGE_INFO(thd, stage_end);
(void)unit->cleanup();
if (is_prepared())
Expand Down
9 changes: 8 additions & 1 deletion sql/sql_update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ bool Sql_cmd_update::update_single_table(THD *thd)
ha_rows dup_key_found;
bool need_sort= TRUE;
bool reverse= FALSE;
ha_rows updated, updated_or_same, found;
ha_rows updated_or_same;
key_map old_covering_keys;
TABLE *table;
SQL_SELECT *select= NULL;
Expand Down Expand Up @@ -3140,6 +3140,13 @@ bool Sql_cmd_update::execute_inner(THD *thd)

if (result)
{
/* In single table case, this->updated set by update_single_table */
if (res && multitable)
{
found= ((multi_update*)get_result())->num_found();
updated= ((multi_update*)get_result())->num_updated();
}

res= false;
delete result;
}
Expand Down
8 changes: 8 additions & 0 deletions sql/sql_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ bool compare_record(const TABLE *table);
class Sql_cmd_update final : public Sql_cmd_dml
{
public:
ha_rows found{0}, updated{0};
Sql_cmd_update(bool multitable_arg)
: orig_multitable(multitable_arg), multitable(multitable_arg)
{}
Expand All @@ -65,6 +66,13 @@ class Sql_cmd_update final : public Sql_cmd_dml

void set_as_multitable() { multitable= true; }

void get_dml_stat (ha_rows &found, ha_rows &changed) override
{

found= this->found;
changed= this->updated;
}

protected:
/**
@brief Perform precheck of table privileges for update statements
Expand Down

0 comments on commit 8505dc7

Please sign in to comment.