Skip to content

Commit 6d76033

Browse files
committed
Bug#21246964: ASAN: MEMORY LEAK IN PROCESS_ALL_TABLES()
Bug#21247377: ASAN: MEMORY LEAK IN SHOW_VARIABLE_QUERY_EXTRACTOR / RUN_SQL_FIX_PRIVILEGE_TABLES() Bug#21253535: ASAN: MEMORY LEAK IN MYSQL_UPGRADE Fix memory leak in mysqlcheck - process_all_tables(). Close result by calling mysql_free_result() after processing. Fix memory leaks related to mysql_upgrade: 1) Allocate Instance_callback instances on stack rather than heap. 2) Always call mysql_close() to deallocate memory. Also fix a valgrind warning, Conditional jump or move depends on uninitialised value(s) in Mysql::Tools::Upgrade::Program::fix_privilage_tables_error() (sic)
1 parent a8da7bd commit 6d76033

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

client/check/mysqlcheck_core.cc

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static int process_all_databases()
8181
if (process_one_db(row[0]))
8282
result = 1;
8383
}
84+
mysql_free_result(tableres);
8485
return result;
8586
}
8687
/* process_all_databases */

client/upgrade/program.cc

+32-20
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ enum exit_codes
7878
EXIT_UPGRADING_QUERIES_ERROR = 5,
7979
};
8080

81+
82+
class Mysql_connection_holder
83+
{
84+
MYSQL* m_mysql_connection;
85+
86+
public:
87+
explicit Mysql_connection_holder(MYSQL *mysql_connection)
88+
: m_mysql_connection(mysql_connection)
89+
{ }
90+
91+
~Mysql_connection_holder()
92+
{
93+
mysql_close(this->m_mysql_connection);
94+
}
95+
};
96+
97+
8198
class Program : public Base::Abstract_connection_program
8299
{
83100
public:
@@ -119,6 +136,8 @@ class Program : public Base::Abstract_connection_program
119136
setbuf(stdout, NULL);
120137

121138
this->m_mysql_connection= this->create_connection();
139+
// Remember to call mysql_close()
140+
Mysql_connection_holder connection_holder(m_mysql_connection);
122141
this->m_query_runner= new Mysql_query_runner(this->m_mysql_connection);
123142
this->m_query_runner->add_message_callback(new Instance_callback
124143
<int, Mysql_message, Program>(this, &Program::print_error));
@@ -434,8 +453,6 @@ class Program : public Base::Abstract_connection_program
434453
/* Create a file indicating upgrade has been performed */
435454
this->create_mysql_upgrade_info_file();
436455

437-
mysql_close(this->m_mysql_connection);
438-
439456
return 0;
440457
}
441458

@@ -525,13 +542,13 @@ class Program : public Base::Abstract_connection_program
525542
const char **query_ptr;
526543
int result;
527544

545+
Instance_callback<int, vector<string>, Program>
546+
result_callback(this, &Program::result_callback);
547+
Instance_callback<int, Mysql_message, Program>
548+
message_callback(this, &Program::fix_privilage_tables_error);
528549
Mysql_query_runner runner(*this->m_query_runner);
529-
runner.add_result_callback(
530-
new Instance_callback<int, vector<string>, Program>(
531-
this, &Program::result_callback));
532-
runner.add_message_callback(
533-
new Instance_callback<int, Mysql_message, Program>(
534-
this, &Program::fix_privilage_tables_error));
550+
runner.add_result_callback(&result_callback);
551+
runner.add_message_callback(&message_callback);
535552

536553
this->print_verbose_message("Running queries to upgrade MySQL server.");
537554

@@ -565,13 +582,13 @@ class Program : public Base::Abstract_connection_program
565582
const char **query_ptr;
566583
int result;
567584

585+
Instance_callback<int, vector<string>, Program>
586+
result_callback(this, &Program::result_callback);
587+
Instance_callback<int, Mysql_message, Program>
588+
message_callback(this, &Program::fix_privilage_tables_error);
568589
Mysql_query_runner runner(*this->m_query_runner);
569-
runner.add_result_callback(
570-
new Instance_callback<int, vector<string>, Program>(
571-
this, &Program::result_callback));
572-
runner.add_message_callback(
573-
new Instance_callback<int, Mysql_message, Program>(
574-
this, &Program::fix_privilage_tables_error));
590+
runner.add_result_callback(&result_callback);
591+
runner.add_message_callback(&message_callback);
575592

576593
this->print_verbose_message("Upgrading the sys schema.");
577594

@@ -744,13 +761,8 @@ class Program : public Base::Abstract_connection_program
744761
*/
745762
int fix_privilage_tables_error(Mysql_message message)
746763
{
747-
String error;
748-
uint dummy_errors;
749-
error.copy("error", 5, &my_charset_latin1,
750-
this->m_mysql_connection->charset, &dummy_errors);
751-
752764
bool is_error = my_strcasecmp(this->m_mysql_connection->charset,
753-
message.severity.c_str(), error.c_ptr()) == 0;
765+
message.severity.c_str(), "error") == 0;
754766

755767
// This if it is error message and if it is not expected one.
756768
if (this->m_temporary_verbose ||

client/upgrade/show_variable_query_extractor.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ int Show_variable_query_extractor::get_variable_value(
3636
Show_variable_query_extractor extractor;
3737
Mysql_query_runner query_runner_to_use(*query_runner_to_copy);
3838

39-
query_runner_to_use.add_result_callback(
40-
new Instance_callback<int, vector<string>, Show_variable_query_extractor>(
41-
&extractor, &Show_variable_query_extractor::extract_variable));
39+
Instance_callback<int, vector<string>, Show_variable_query_extractor>
40+
callback(&extractor, &Show_variable_query_extractor::extract_variable);
41+
query_runner_to_use.add_result_callback(&callback);
4242

4343
if (query_runner_to_use.run_query("SELECT @@global." + variable))
4444
return 1;

0 commit comments

Comments
 (0)