From ce7cc49465a0f45db8a3949a84612c4b7cfa1021 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Fri, 28 Apr 2023 18:09:19 +0200 Subject: [PATCH] test_server: Use dynamically allocated state The "dynamically" loaded server is using allocated state and using something else complicates proper cleanup. Signed-off-by: Jakub Jelen Reviewed-by: Norbert Pocs --- tests/server/test_server/main.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tests/server/test_server/main.c b/tests/server/test_server/main.c index ff67dd9f..39c01223 100644 --- a/tests/server/test_server/main.c +++ b/tests/server/test_server/main.c @@ -596,9 +596,12 @@ int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv)) .address = NULL, .with_global_config = true, }; - struct server_state_st state = { - .address = NULL, - }; + struct server_state_st *state = calloc(1, sizeof(struct server_state_st)); + + if (state == NULL) { + printf("Failed to allocate memory\n"); + return -1; + } #ifdef HAVE_ARGP_H argp_parse (&argp, argc, argv, 0, 0, &arguments); @@ -608,6 +611,8 @@ int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv)) pid_file = fopen(arguments.pid_file, "w"); if (pid_file == NULL) { rc = -1; + free_server_state(state); + SAFE_FREE(state); goto free_arguments; } pid = getpid(); @@ -616,22 +621,19 @@ int main(UNUSED_PARAM(int argc), UNUSED_PARAM(char **argv)) } /* Initialize the state using default or given parameters */ - rc = init_server_state(&state, &arguments); + rc = init_server_state(state, &arguments); if (rc != 0) { + free_server_state(state); + SAFE_FREE(state); goto free_arguments; } /* Free the arguments used to initialize the state before fork */ free_arguments(&arguments); - /* Run the server */ - rc = run_server(&state); - if (rc != 0) { - goto free_state; - } + /* Run the server: Frees the state in all processes */ + rc = run_server(state); -free_state: - free_server_state(&state); free_arguments: free_arguments(&arguments); return rc;