fix(test/tap): dead cleanup label in mysql_hostgroup_attributes_config_file-t.cpp

Same pattern as the sibling servers_defaults test: -Wunused-label
flagged the 'cleanup:' block, which held the only restore-from-backup
path for the mysql_hostgroup_attributes runtime table. Nothing ever
jumped to it, so every MYSQL_QUERY_T early-return from the test body
skipped the restore and leaked the admin MYSQL* handle.

- Add mysql_init NULL check and close admin on real_connect failure.
- Introduce a run_cleanup_and_exit lambda that performs the
  DELETE/INSERT restore (conditional on backup_created) and closes
  admin.
- Replace the MYSQL_QUERY_T backup-creation calls with explicit
  mysql_query_t + run_cleanup_and_exit so any failure goes through
  the cleanup path.
- Delete the dead 'cleanup:' label.
lint-tap-tests-static-analysis
Rene Cannao 2 months ago
parent 14e72d04a2
commit fcfeec42a9

@ -235,24 +235,56 @@ int main(int, char**) {
}
MYSQL* admin = mysql_init(NULL);
if (!admin) {
fprintf(stderr, "File %s, line %d, Error: mysql_init failed for admin\n",
__FILE__, __LINE__);
return EXIT_FAILURE;
}
if (!mysql_real_connect(admin, cl.host, cl.admin_username, cl.admin_password, NULL, cl.admin_port, NULL, 0)) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin));
mysql_close(admin);
return EXIT_FAILURE;
}
// For cleanup
MYSQL_QUERY_T(admin, "DROP TABLE IF EXISTS mysql_hostgroup_attributes_0508");
MYSQL_QUERY_T(admin, "CREATE TABLE mysql_hostgroup_attributes_0508 AS SELECT * FROM mysql_hostgroup_attributes");
// Track whether the backup table has been created; only then should
// the restore path run.
bool backup_created = false;
// Runs the restore (if the backup exists) and closes admin. Every
// error path after the backup is created must go through this lambda;
// the previous version had a 'cleanup:' label nothing ever jumped to,
// which meant every test failure leaked admin and left the
// mysql_hostgroup_attributes table corrupted for downstream tests.
auto run_cleanup_and_exit = [&]() -> int {
if (backup_created) {
if (mysql_query_t(admin, "DELETE FROM mysql_hostgroup_attributes")) {
fprintf(stderr, "File %s, line %d, Error: %s\n",
__FILE__, __LINE__, mysql_error(admin));
}
if (mysql_query_t(admin, "INSERT INTO mysql_hostgroup_attributes SELECT * FROM mysql_hostgroup_attributes_0508")) {
fprintf(stderr, "File %s, line %d, Error: %s\n",
__FILE__, __LINE__, mysql_error(admin));
}
}
mysql_close(admin);
return exit_status();
};
// For cleanup: create a backup of the current runtime state. Use
// explicit mysql_query_t so failures go through run_cleanup_and_exit
// rather than being swallowed by MYSQL_QUERY_T's early return.
if (mysql_query_t(admin, "DROP TABLE IF EXISTS mysql_hostgroup_attributes_0508")) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin));
return run_cleanup_and_exit();
}
if (mysql_query_t(admin, "CREATE TABLE mysql_hostgroup_attributes_0508 AS SELECT * FROM mysql_hostgroup_attributes")) {
fprintf(stderr, "File %s, line %d, Error: %s\n", __FILE__, __LINE__, mysql_error(admin));
return run_cleanup_and_exit();
}
backup_created = true;
validate_mysql_hostgroup_attributes_from_config(admin);
write_mysql_hostgroup_attributes_to_config(admin);
cleanup:
MYSQL_QUERY_T(admin, "DELETE FROM mysql_hostgroup_attributes");
MYSQL_QUERY_T(admin, "INSERT INTO mysql_hostgroup_attributes SELECT * FROM mysql_hostgroup_attributes_0508");
mysql_close(admin);
return exit_status();
return run_cleanup_and_exit();
}
Loading…
Cancel
Save