From d26072fd2b1d2c9a5e11498ec71643cbb422b1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Thu, 7 Oct 2021 22:01:22 +0200 Subject: [PATCH] Several memory error fixes for test files reported by ASAN --- test/tap/tests/admin_various_commands2-t.cpp | 2 +- test/tap/tests/firewall_commands1-t.cpp | 2 +- .../reg_test_3427-stmt_first_comment1-t.cpp | 18 +++++++++--------- .../reg_test_3546-stmt_empty_params-t.cpp | 17 +++++++++-------- test/tap/tests/test_mysqlsh-t.cpp | 2 -- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/test/tap/tests/admin_various_commands2-t.cpp b/test/tap/tests/admin_various_commands2-t.cpp index b37e39d6a..3f3b73da7 100644 --- a/test/tap/tests/admin_various_commands2-t.cpp +++ b/test/tap/tests/admin_various_commands2-t.cpp @@ -166,7 +166,7 @@ int main() { for (std::vector::iterator it2 = queries.begin(); it2 != queries.end(); it2++) { - proxysql_admin = mysql_init(NULL); // local scope + MYSQL* proxysql_admin = mysql_init(NULL); // local scope if (!proxysql_admin) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); return -1; diff --git a/test/tap/tests/firewall_commands1-t.cpp b/test/tap/tests/firewall_commands1-t.cpp index 51611e434..f3bfd1a63 100644 --- a/test/tap/tests/firewall_commands1-t.cpp +++ b/test/tap/tests/firewall_commands1-t.cpp @@ -85,7 +85,7 @@ int main() { plan(queries.size()); for (std::vector::iterator it2 = queries.begin(); it2 != queries.end(); it2++) { - proxysql_admin = mysql_init(NULL); // local scope . We intentionally create new connections + MYSQL* proxysql_admin = mysql_init(NULL); // local scope . We intentionally create new connections if (!proxysql_admin) { fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(proxysql_admin)); return -1; diff --git a/test/tap/tests/reg_test_3427-stmt_first_comment1-t.cpp b/test/tap/tests/reg_test_3427-stmt_first_comment1-t.cpp index 91bed356e..e87150a43 100644 --- a/test/tap/tests/reg_test_3427-stmt_first_comment1-t.cpp +++ b/test/tap/tests/reg_test_3427-stmt_first_comment1-t.cpp @@ -77,7 +77,6 @@ int main(int argc, char** argv) { } } - MYSQL_STMT* stmt = nullptr; MYSQL* proxysql_mysql = mysql_init(NULL); MYSQL* proxysql_admin = mysql_init(NULL); @@ -91,13 +90,6 @@ int main(int argc, char** argv) { return -1; } - stmt = mysql_stmt_init(proxysql_mysql); - if (!stmt) { - diag("mysql_stmt_init(), out of memory"); - res = EXIT_FAILURE; - goto exit; - } - // Insert data in the table to be queried // ************************************************************************* @@ -164,6 +156,13 @@ int main(int argc, char** argv) { std::string query {}; string_format(query_t, query, query_id); + MYSQL_STMT* stmt = mysql_stmt_init(proxysql_mysql); + if (!stmt) { + diag("mysql_stmt_init(), out of memory"); + res = EXIT_FAILURE; + goto exit; + } + if (mysql_stmt_prepare(stmt, query.c_str(), strlen(query.c_str()))) { diag("mysql_stmt_prepare at line %d failed: %s", __LINE__ , mysql_error(proxysql_mysql)); mysql_close(proxysql_mysql); @@ -261,11 +260,12 @@ int main(int argc, char** argv) { res = EXIT_FAILURE; goto exit; } + + mysql_stmt_close(stmt); } } exit: - if (stmt) { mysql_stmt_close(stmt); } mysql_close(proxysql_mysql); mysql_close(proxysql_admin); diff --git a/test/tap/tests/reg_test_3546-stmt_empty_params-t.cpp b/test/tap/tests/reg_test_3546-stmt_empty_params-t.cpp index 2f07ca164..cf2f35eb7 100644 --- a/test/tap/tests/reg_test_3546-stmt_empty_params-t.cpp +++ b/test/tap/tests/reg_test_3546-stmt_empty_params-t.cpp @@ -89,7 +89,6 @@ int main(int argc, char** argv) { return -1; } - MYSQL_STMT* stmt_param = nullptr; MYSQL* proxysql_mysql = mysql_init(NULL); MYSQL* proxysql_admin = mysql_init(NULL); @@ -103,12 +102,6 @@ int main(int argc, char** argv) { return -1; } - stmt_param = mysql_stmt_init(proxysql_mysql); - if (!stmt_param) { - diag("mysql_stmt_init(), out of memory"); - goto exit; - } - // Insert the row to be queried with the prepared statement. // ************************************************************************* MYSQL_QUERY(proxysql_mysql, "CREATE DATABASE IF NOT EXISTS test"); @@ -132,6 +125,13 @@ int main(int argc, char** argv) { } { + MYSQL_STMT* stmt_param = nullptr; + stmt_param = mysql_stmt_init(proxysql_mysql); + if (!stmt_param) { + diag("mysql_stmt_init(), out of memory"); + goto exit; + } + // Set the number of maximum connections for servers in the writer hostgroup std::string t_update_mysql_servers { "UPDATE mysql_servers SET max_connections=1 WHERE hostgroup_id=%d" @@ -246,10 +246,11 @@ int main(int argc, char** argv) { ); } } + + mysql_stmt_close(stmt_param); } exit: - if (stmt_param) { mysql_stmt_close(stmt_param); } mysql_close(proxysql_mysql); mysql_close(proxysql_admin); diff --git a/test/tap/tests/test_mysqlsh-t.cpp b/test/tap/tests/test_mysqlsh-t.cpp index 98ccaa5c7..e8879f207 100644 --- a/test/tap/tests/test_mysqlsh-t.cpp +++ b/test/tap/tests/test_mysqlsh-t.cpp @@ -89,8 +89,6 @@ int main(int argc, char** argv) { } else { ok(false, "Invalid resulset. Expected 'num_fields' = 1, not %d", concat_num_fields); } - - mysql_free_result(concat_res); } mysql_free_result(concat_res);