From a582bb325288fe2a9cbc25f6437263bd7c472f41 Mon Sep 17 00:00:00 2001 From: Weston Schmidt Date: Tue, 15 Sep 2020 18:04:41 -0700 Subject: [PATCH] Fix several compilation errors (multiple definitions of global variables, remove overlapping strncpy() strings which cause latent bugs) to allow for code coverage to work. --- src/ParodusInternal.h | 1 - src/config.c | 7 ++++++- src/connection.c | 2 ++ src/connection.h | 2 +- src/main.c | 2 +- tests/test_auth_token.c | 4 +++- tests/test_client_list.c | 2 ++ tests/test_config.c | 15 ++++++++++++++- tests/test_conn_interface.c | 3 ++- tests/test_connection.c | 2 -- tests/test_crud_internal.c | 3 --- tests/test_nopoll_handlers.c | 3 --- tests/test_nopoll_handlers_fragment.c | 3 --- tests/test_service_alive.c | 1 + tests/test_token.c | 1 + 15 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/ParodusInternal.h b/src/ParodusInternal.h index 7cc6259..f8a1193 100644 --- a/src/ParodusInternal.h +++ b/src/ParodusInternal.h @@ -139,7 +139,6 @@ typedef struct { /*----------------------------------------------------------------------------*/ extern bool g_shutdown; extern ParodusMsg *ParodusMsgQ; -int numLoops; /*----------------------------------------------------------------------------*/ /* Function Prototypes */ /*----------------------------------------------------------------------------*/ diff --git a/src/config.c b/src/config.c index 35b0f5b..b710b8a 100644 --- a/src/config.c +++ b/src/config.c @@ -214,8 +214,13 @@ int parse_webpa_url__ (const char *full_url, if(openBracket != NULL){ //Remove [ from server address char *remove = server_addr; + int i; + + // Strings can overlap, so don't use strncpy() remove++; - parStrncpy (server_addr, remove, server_addr_buflen); + for( i = 0; i < server_addr_buflen; i++ ) { + server_addr[i] = remove[i]; + } closeBracket = strchr(server_addr,']'); if(closeBracket != NULL){ //Remove ] by making it as null diff --git a/src/connection.c b/src/connection.c index 466f6d3..c679870 100644 --- a/src/connection.c +++ b/src/connection.c @@ -62,6 +62,8 @@ enum { /* File Scoped Variables */ /*----------------------------------------------------------------------------*/ +parodusOnPingStatusChangeHandler on_ping_status_change; + pthread_mutex_t backoff_delay_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t backoff_delay_con=PTHREAD_COND_INITIALIZER; diff --git a/src/connection.h b/src/connection.h index b43ebe1..d750829 100644 --- a/src/connection.h +++ b/src/connection.h @@ -44,7 +44,7 @@ extern "C" { * i.e. ping_miss or ping receive after miss */ typedef void (*parodusOnPingStatusChangeHandler) (char * status); -parodusOnPingStatusChangeHandler on_ping_status_change; +extern parodusOnPingStatusChangeHandler on_ping_status_change; /*----------------------------------------------------------------------------*/ /* Function Prototypes */ diff --git a/src/main.c b/src/main.c index 85c3010..b97a471 100644 --- a/src/main.c +++ b/src/main.c @@ -42,7 +42,7 @@ typedef void Sigfunc(int); /*----------------------------------------------------------------------------*/ /* File Scoped Variables */ /*----------------------------------------------------------------------------*/ -/* none */ +int numLoops; /*----------------------------------------------------------------------------*/ /* Function Prototypes */ diff --git a/tests/test_auth_token.c b/tests/test_auth_token.c index 2438c70..8c0e3cf 100644 --- a/tests/test_auth_token.c +++ b/tests/test_auth_token.c @@ -54,7 +54,9 @@ int curl_easy_perform(CURL *curl) write_callback_fn (msg, 1, strlen(msg), &test_data); return rtn; } -int g_response_code=0; + +extern int g_response_code; + void setGlobalResponseCode (int response_code) { g_response_code = response_code; diff --git a/tests/test_client_list.c b/tests/test_client_list.c index e38da34..4ebe551 100644 --- a/tests/test_client_list.c +++ b/tests/test_client_list.c @@ -32,6 +32,8 @@ pthread_t test_tid2; static void *client_rcv_task(); static void *client2_rcv_task(); +int numLoops; + /*----------------------------------------------------------------------------*/ /* Tests */ /*----------------------------------------------------------------------------*/ diff --git a/tests/test_config.c b/tests/test_config.c index e9bf864..f53f6ad 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -116,6 +116,8 @@ void test_setParodusConfig() assert_string_equal(cfg.jwt_key, temp->jwt_key); #endif assert_string_equal(cfg.crud_config_file, temp->crud_config_file); + + free(cfg.crud_config_file); } void test_getParodusConfig() @@ -299,8 +301,9 @@ void test_loadParodusCfg() { ParodusCfg tmpcfg; ParodusCfg *Cfg = NULL; - Cfg = (ParodusCfg*)malloc(sizeof(ParodusCfg)); char protocol[32] = {'\0'}; + Cfg = (ParodusCfg*)malloc(sizeof(ParodusCfg)); + memset(Cfg, 0, sizeof(ParodusCfg)); parStrncpy(Cfg->hw_model, "TG1682", sizeof(Cfg->hw_model)); parStrncpy(Cfg->hw_serial_number, "Fer23u948590", sizeof(Cfg->hw_serial_number)); @@ -351,6 +354,14 @@ void test_loadParodusCfg() assert_string_equal(tmpcfg.seshat_url, "ipc://tmp/seshat_service.url"); #endif assert_string_equal(tmpcfg.crud_config_file, "parodus_cfg.json"); + + free(tmpcfg.client_cert_path); + free(tmpcfg.token_server_url); + free(tmpcfg.crud_config_file); + + free(Cfg->crud_config_file); + free(Cfg->client_cert_path); + free(Cfg->token_server_url); free(Cfg); } @@ -422,6 +433,8 @@ void test_setDefaultValuesToCfg() assert_string_equal(cfg->webpa_path_url, WEBPA_PATH_URL); assert_string_equal(cfg->webpa_uuid, "1234567-345456546"); assert_string_equal(cfg->cloud_status, CLOUD_STATUS_OFFLINE); + + free(cfg); } void err_setDefaultValuesToCfg() diff --git a/tests/test_conn_interface.c b/tests/test_conn_interface.c index 181c04d..5433e7c 100644 --- a/tests/test_conn_interface.c +++ b/tests/test_conn_interface.c @@ -42,7 +42,8 @@ pthread_mutex_t nano_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t nano_con=PTHREAD_COND_INITIALIZER; pthread_mutex_t svc_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t svc_con=PTHREAD_COND_INITIALIZER; - +int numLoops; +parodusOnPingStatusChangeHandler on_ping_status_change; /*----------------------------------------------------------------------------*/ /* Mocks */ diff --git a/tests/test_connection.c b/tests/test_connection.c index 2607c93..f6c1f64 100644 --- a/tests/test_connection.c +++ b/tests/test_connection.c @@ -62,10 +62,8 @@ extern int keep_trying_to_connect (create_connection_ctx_t *ctx, /* File Scoped Variables */ /*----------------------------------------------------------------------------*/ -bool close_retry; bool LastReasonStatus; bool interface_down_event = false; -pthread_mutex_t close_mut; pthread_mutex_t interface_down_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t interface_down_con=PTHREAD_COND_INITIALIZER; diff --git a/tests/test_crud_internal.c b/tests/test_crud_internal.c index 465ae76..332d282 100644 --- a/tests/test_crud_internal.c +++ b/tests/test_crud_internal.c @@ -32,9 +32,6 @@ #include "../src/connection.h" #include "../src/close_retry.h" -bool LastReasonStatus; -pthread_mutex_t close_mut; - char *get_global_reconnect_reason() { return "parodus_stopping"; diff --git a/tests/test_nopoll_handlers.c b/tests/test_nopoll_handlers.c index e463081..a4b20b2 100644 --- a/tests/test_nopoll_handlers.c +++ b/tests/test_nopoll_handlers.c @@ -28,14 +28,11 @@ /*----------------------------------------------------------------------------*/ /* File Scoped Variables */ /*----------------------------------------------------------------------------*/ -volatile unsigned int heartBeatTimer; bool LastReasonStatus; bool interface_down_event = false; int closeReason = 0; -pthread_mutex_t close_mut; pthread_mutex_t interface_down_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t interface_down_con=PTHREAD_COND_INITIALIZER; -bool close_retry; /*----------------------------------------------------------------------------*/ /* Mocks */ /*----------------------------------------------------------------------------*/ diff --git a/tests/test_nopoll_handlers_fragment.c b/tests/test_nopoll_handlers_fragment.c index 0d6fc9d..4c79faf 100644 --- a/tests/test_nopoll_handlers_fragment.c +++ b/tests/test_nopoll_handlers_fragment.c @@ -30,10 +30,7 @@ /*----------------------------------------------------------------------------*/ /* File Scoped Variables */ /*----------------------------------------------------------------------------*/ -volatile unsigned int heartBeatTimer; bool LastReasonStatus; -pthread_mutex_t close_mut; -bool close_retry; /*----------------------------------------------------------------------------*/ /* Mocks */ /*----------------------------------------------------------------------------*/ diff --git a/tests/test_service_alive.c b/tests/test_service_alive.c index c1f5433..7fd3aa3 100644 --- a/tests/test_service_alive.c +++ b/tests/test_service_alive.c @@ -32,6 +32,7 @@ static void *client_rcv_task(); static void *keep_alive_thread(); static void add_client(); int sock1; +int numLoops; pthread_t threadId; pthread_mutex_t crud_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t crud_con=PTHREAD_COND_INITIALIZER; diff --git a/tests/test_token.c b/tests/test_token.c index f6206af..ac05551 100644 --- a/tests/test_token.c +++ b/tests/test_token.c @@ -160,6 +160,7 @@ extern unsigned int get_algo_mask (const char *algo_str); pthread_mutex_t crud_mut=PTHREAD_MUTEX_INITIALIZER; pthread_cond_t crud_con=PTHREAD_COND_INITIALIZER; +int numLoops; pthread_cond_t *get_global_crud_con(void) {