1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-27 18:02:13 +03:00

MDEV-30143 Segfault on select query using index for group-by and filesort

The problem was trying to access JOIN_TAB::select which is set to NULL
when using the filesort. The correct way is accessing either
JOIN_TAB::select or JOIN_TAB::filesort->select depending on whether
the filesort is used.
This commit introduces member function JOIN_TAB::get_sql_select()
encapsulating that check so the code duplication is eliminated.

The new condition (s->table->quick_keys.is_set(best_key->key))
was added to  best_access_path() to eliminate a Valgrind error.
The cause of that error was using TRASH_ALLOC(quick_key_parts)
instead of bzero(quick_key_parts); hence, accessing
s->table->quick_key_parts[best_key->key]) without prior checking
for quick_keys.is_set() might have caused reading "dirty" memory
This commit is contained in:
Oleg Smirnov
2023-01-29 19:39:14 +07:00
parent 131ef14a6e
commit 60f0765b58
4 changed files with 54 additions and 6 deletions

View File

@ -8120,6 +8120,7 @@ best_access_path(JOIN *join,
if ((records >= s->found_records || best > s->read_time) && // (1)
!(best_key && best_key->key == MAX_KEY) && // (2)
!(s->quick && best_key && s->quick->index == best_key->key && // (2)
s->table->quick_keys.is_set(best_key->key) && // (2)
best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
!((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) && // (3)
! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
@ -13791,7 +13792,7 @@ double JOIN_TAB::scan_time()
ha_rows JOIN_TAB::get_examined_rows()
{
double examined_rows;
SQL_SELECT *sel= filesort? filesort->select : this->select;
const SQL_SELECT *sel= get_sql_select();
if (sel && sel->quick && use_quick != 2)
examined_rows= (double)sel->quick->records;
@ -26858,13 +26859,12 @@ bool JOIN_TAB::save_explain_data(Explain_table_access *eta,
eta->key.clear();
eta->quick_info= NULL;
SQL_SELECT *tab_select;
/*
We assume that if this table does pre-sorting, then it doesn't do filtering
with SQL_SELECT.
*/
DBUG_ASSERT(!(select && filesort));
tab_select= (filesort)? filesort->select : select;
const SQL_SELECT *tab_select= get_sql_select();
if (filesort)
{