MXS-1709 Fix memory leaks in unit tests

This commit is contained in:
Marko
2018-06-06 22:59:52 +03:00
parent 3c0f301674
commit 0dd7448586
2 changed files with 72 additions and 13 deletions

View File

@ -125,7 +125,7 @@ int test(StorageFactory& factory, istream& in)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int rv = EXIT_FAILURE; int rv = EXIT_FAILURE;
StorageFactory* pFactory;
if ((argc == 2) || (argc == 3)) if ((argc == 2) || (argc == 3))
{ {
char* libdir = MXS_STRDUP("../../../../../query_classifier/qc_sqlite/"); char* libdir = MXS_STRDUP("../../../../../query_classifier/qc_sqlite/");
@ -139,7 +139,7 @@ int main(int argc, char* argv[])
libdir = MXS_STRDUP("../storage/storage_inmemory/"); libdir = MXS_STRDUP("../storage/storage_inmemory/");
set_libdir(libdir); set_libdir(libdir);
StorageFactory* pFactory = StorageFactory::Open(zModule); pFactory = StorageFactory::Open(zModule);
if (pFactory) if (pFactory)
{ {
@ -182,6 +182,7 @@ int main(int argc, char* argv[])
// TODO: Remove this once globally allocated memory is freed // TODO: Remove this once globally allocated memory is freed
MXS_FREE(libdir); MXS_FREE(libdir);
delete pFactory;
} }
else else
{ {

View File

@ -54,6 +54,7 @@
static void printVersion(const char *progname); static void printVersion(const char *progname);
static void printUsage(const char *progname); static void printUsage(const char *progname);
static void master_free_parsed_options(CHANGE_MASTER_OPTIONS *options);
extern int blr_test_parse_change_master_command(char *input, char *error_string, extern int blr_test_parse_change_master_command(char *input, char *error_string,
CHANGE_MASTER_OPTIONS *config); CHANGE_MASTER_OPTIONS *config);
extern char *blr_test_set_master_logfile(ROUTER_INSTANCE *router, char *filename, char *error); extern char *blr_test_set_master_logfile(ROUTER_INSTANCE *router, char *filename, char *error);
@ -76,6 +77,12 @@ int main(int argc, char **argv)
int rc; int rc;
char error_string[BINLOG_ERROR_MSG_LEN + 1] = ""; char error_string[BINLOG_ERROR_MSG_LEN + 1] = "";
CHANGE_MASTER_OPTIONS change_master; CHANGE_MASTER_OPTIONS change_master;
change_master.host = NULL;
change_master.port = NULL;
change_master.user = NULL;
change_master.password = NULL;
change_master.binlog_file = NULL;
change_master.binlog_pos = NULL;
char query[512 + 1] = ""; char query[512 + 1] = "";
char saved_query[512 + 1] = ""; char saved_query[512 + 1] = "";
int command_offset = strlen("CHANGE MASTER TO"); int command_offset = strlen("CHANGE MASTER TO");
@ -329,7 +336,7 @@ int main(int argc, char **argv)
} }
tests++; tests++;
master_free_parsed_options(&change_master);
/** /**
* Test 9: 1 valid option with value * Test 9: 1 valid option with value
* *
@ -348,7 +355,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -369,7 +376,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -390,7 +397,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -411,7 +418,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -433,7 +440,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -455,7 +462,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -478,7 +485,7 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid / not valid options for [%s]\n", tests, saved_query);
} }
master_free_parsed_options(&change_master);
tests++; tests++;
/** /**
@ -502,6 +509,16 @@ int main(int argc, char **argv)
{ {
printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query); printf("Test %d PASSED, valid options for [%s]\n", tests, saved_query);
} }
MXS_FREE(change_master.host);
change_master.host = NULL;
MXS_FREE(change_master.port);
change_master.port = NULL;
MXS_FREE(change_master.user);
change_master.user = NULL;
MXS_FREE(change_master.password);
change_master.password = NULL;
MXS_FREE(change_master.binlog_pos);
change_master.binlog_pos = NULL;
tests++; tests++;
@ -509,7 +526,7 @@ int main(int argc, char **argv)
/** /**
* Test 17: use current binlog filename in master_state != BLRM_UNCONFIGURED * Test 17: use current binlog filename in master_state != BLRM_UNCONFIGURED
* and try to set a new filename with wront format from previous test * and try to set a new filename with wrong format from previous test
* *
* Expected master_log_file is NULL and error_string is not empty * Expected master_log_file is NULL and error_string is not empty
*/ */
@ -539,7 +556,8 @@ int main(int argc, char **argv)
} }
tests++; tests++;
MXS_FREE(change_master.binlog_file);
change_master.binlog_file = NULL;
printf("--- MASTER_LOG_POS and MASTER_LOG_FILE rule/constraints checks ---\n"); printf("--- MASTER_LOG_POS and MASTER_LOG_FILE rule/constraints checks ---\n");
/******************************************** /********************************************
@ -845,8 +863,48 @@ int main(int argc, char **argv)
mxs_log_flush_sync(); mxs_log_flush_sync();
mxs_log_finish(); mxs_log_finish();
MXS_FREE(inst->user);
MXS_FREE(inst->password);
MXS_FREE(inst->fileroot);
MXS_FREE(inst); MXS_FREE(inst);
MXS_FREE(roptions); MXS_FREE(roptions);
return 0; return 0;
} }
static void
master_free_parsed_options(CHANGE_MASTER_OPTIONS *options)
{
if (options->host)
{
MXS_FREE(options->host);
options->host = NULL;
}
if (options->port)
{
MXS_FREE(options->port);
options->port = NULL;
}
if (options->user)
{
MXS_FREE(options->user);
options->user = NULL;
}
if (options->password)
{
MXS_FREE(options->password);
options->password = NULL;
}
if (options->binlog_file)
{
MXS_FREE(options->binlog_file);
options->binlog_file = NULL;
}
if (options->binlog_pos)
{
MXS_FREE(options->binlog_pos);
options->binlog_pos = NULL;
}
}