diff --git a/sql/item.cc b/sql/item.cc index 8d924644b35c..a13678cff143 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -643,10 +643,10 @@ bool Item::aggregate_type(const char *name, Item **items, uint count) { if (mixed_signs && is_integer_type(new_type)) { bool bump_range = false; new_unsigned = false; - for (uint i = 0; i < count; i++) - bump_range |= (items[i]->unsigned_flag && - (items[i]->data_type() == new_type || - items[i]->data_type() == MYSQL_TYPE_BIT)); + for (uint i = 0; i <= count; i++) + bump_range |= (items[i]->unsigned_flag && + (items[i]->data_type() == new_type || + items[i]->data_type() == MYSQL_TYPE_BIT)); if (bump_range) { switch (new_type) { case MYSQL_TYPE_TINY: diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 2b06d5f5532c..5063aa94347f 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -1127,9 +1127,12 @@ OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild) { wild, strlen(wild), false)) continue; + char *temp_name = new char[share->table_name.length + 1]; + strcpy(temp_name, share->table_name.str); + if (!(*start_list = (OPEN_TABLE_LIST *)(*THR_MALLOC) - ->Alloc(sizeof(**start_list) + - share->table_cache_key.length))) { + ->Alloc(sizeof(**start_list) + + share->table_cache_key.length))) { open_list = nullptr; // Out of memory break; } @@ -1144,6 +1147,7 @@ OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild) { (*start_list)->locked = 0; /* Obsolete. */ start_list = &(*start_list)->next; *start_list = nullptr; + delete[] temp_name; } table_cache_manager.unlock_all_and_tdc(); diff --git a/sql/sql_error.cc b/sql/sql_error.cc index b8d85c273712..13df4b77333a 100644 --- a/sql/sql_error.cc +++ b/sql/sql_error.cc @@ -210,7 +210,7 @@ static void copy_string(MEM_ROOT *mem_root, String *dst, const String *src) { if (len) { char *copy = (char *)mem_root->Alloc(len + 1); if (copy) { - memcpy(copy, src->ptr(), len); + memcpy(copy, src->ptr(), len + 1); copy[len] = '\0'; dst->set(copy, len, src->charset()); } diff --git a/sql/sql_executor.cc b/sql/sql_executor.cc index f9cf69ef273b..a00213ecd06e 100644 --- a/sql/sql_executor.cc +++ b/sql/sql_executor.cc @@ -380,7 +380,7 @@ bool has_rollup_result(Item *item) { return has_rollup_result(down_cast(item)->example); } else if (item->type() == Item::FUNC_ITEM) { Item_func *item_func = down_cast(item); - for (uint i = 0; i < item_func->arg_count; i++) { + for (uint i = 0; i <= item_func->arg_count; i++) { if (has_rollup_result(item_func->arguments()[i])) return true; } } else if (item->type() == Item::COND_ITEM) { diff --git a/sql/sql_optimizer.cc b/sql/sql_optimizer.cc index 4fb77d774ae3..64cd1777a1f0 100644 --- a/sql/sql_optimizer.cc +++ b/sql/sql_optimizer.cc @@ -256,6 +256,7 @@ static void SaveCondEqualLists(COND_EQUAL *cond_equal) { } cond_equal->current_level = std::move(copy); SaveCondEqualLists(cond_equal->upper_levels); + if (!copy.empty()) cond_equal->current_level.push_back(copy.front()); } bool JOIN::check_access_path_with_fts() const { diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 5055efb947d7..e29812e0d7f3 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -1898,7 +1898,7 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data, .com_stat[SQLCOM_CHANGE_DB]++; thd->convert_string(&tmp, system_charset_info, com_data->com_init_db.db_name, - com_data->com_init_db.length, thd->charset()); + com_data->com_init_db.length + 1, thd->charset()); const LEX_CSTRING tmp_cstr = {tmp.str, tmp.length}; if (!mysql_change_db(thd, tmp_cstr, false)) { @@ -2120,7 +2120,7 @@ bool dispatch_command(THD *thd, const COM_DATA *com_data, const LEX_CSTRING orig_query = thd->query(); Parser_state parser_state; - if (parser_state.init(thd, thd->query().str, thd->query().length)) { + if (parser_state.init(thd, thd->query().str, thd->query().length + 1)) { MYSQL_NOTIFY_STATEMENT_QUERY_ATTRIBUTES(thd->m_statement_psi, false); break; } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index dd85d54c0cea..7c357510ea03 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -267,17 +267,17 @@ bool reads_not_secondary_columns(const LEX *lex, // Check all read columns of table. for (unsigned int i = bitmap_get_first_set(tl->table->read_set); - i != MY_BIT_NONE; i = bitmap_get_next_set(tl->table->read_set, i)) { - if (tl->table->field[i]->is_flag_set(NOT_SECONDARY_FLAG)) { + i != MY_BIT_NONE; i = bitmap_get_next_set(tl->table->read_set, i + 1)) { + if (tl->table->field[i + 1]->is_flag_set(NOT_SECONDARY_FLAG)) { assert(not_secondary_col_str != nullptr && not_secondary_col_str->empty()); *not_secondary_col_str = - std::string_view{tl->table->field[i]->field_name}; + std::string_view{tl->table->field[i + 1]->field_name}; Opt_trace_context *trace = &lex->thd->opt_trace; if (trace->is_started()) { std::string message(""); message.append("Column "); - message.append(tl->table->field[i]->field_name); + message.append(tl->table->field[i + 1]->field_name); message.append(" is marked as NOT SECONDARY."); const Opt_trace_object trace_wrapper(trace); Opt_trace_object oto(trace, "secondary_engine_not_used"); diff --git a/sql/sql_table.cc b/sql/sql_table.cc index e6ffac31a76a..735adaf75686 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -850,9 +850,9 @@ size_t tablename_to_filename(const char *from, char *to, size_t to_length) { length = strconvert(system_charset_info, from, &my_charset_filename, to, to_length, &errors); - if (check_if_legal_tablename(to) && length + 4 < to_length) { - memcpy(to + length, "@@@", 4); - length += 3; + if (check_if_legal_tablename(to) && length + 3 < to_length) { + memcpy(to + length, "@@@", 5); + length += 4; } DBUG_PRINT("exit", ("to '%s'", to)); return length; diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 4ea98d637fee..0cc62a89cafa 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -272,7 +272,7 @@ bool compare_records(const TABLE *table) { ((table->record[1][null_byte_index]) & field->null_bit)) return true; } - if (field->cmp_binary_offset(table->s->rec_buff_length)) return true; + if (field->cmp_binary_offset(table->s->rec_buff_length + 1)) return true; } } return false;