From e9f3d7f898b58de5a6ec2fb73beb4748d924b0f9 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Wed, 8 Mar 2017 13:50:38 -0700 Subject: [PATCH] add the function ERR remove state and test for it --- src/ssl.c | 22 +++++++++++++++------- tests/api.c | 30 ++++++++++++++++++++++++++++++ wolfcrypt/src/logging.c | 32 ++++++++++++++++++++++++++++++++ wolfssl/wolfcrypt/logging.h | 1 + 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index d9eef8a8a..baf086e65 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -14138,6 +14138,21 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md) } + /* frees all nodes in the current threads error queue + * + * id thread id. ERR_remove_state is depriciated and id is ignored. The + * current threads queue will be free'd. + */ + void wolfSSL_ERR_remove_state(unsigned long id) + { + WOLFSSL_ENTER("wolfSSL_ERR_remove_state"); + (void)id; + if (wc_ERR_remove_state() != 0) { + WOLFSSL_MSG("Error with removing the state"); + } + } + + int wolfSSL_RAND_status(void) { return WOLFSSL_SUCCESS; /* wolfCrypt provides enough seed internally */ @@ -14313,13 +14328,6 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md) } - void wolfSSL_ERR_remove_state(unsigned long state) - { - /* TODO: GetErrors().Remove(); */ - (void)state; - } - - void wolfSSL_EVP_cleanup(void) { /* nothing to do here */ diff --git a/tests/api.c b/tests/api.c index a9e63508f..bd5cb51c8 100644 --- a/tests/api.c +++ b/tests/api.c @@ -14189,6 +14189,36 @@ static void test_wolfSSL_ERR_peek_last_error_line(void) FreeTcpReady(&ready); + /* check clearing error state */ + ERR_remove_state(0); + AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0); + ERR_peek_last_error_line(NULL, &line); + AssertIntEQ(line, 0); + ERR_peek_last_error_line(&file, NULL); + AssertNull(file); + + /* retry connection to fill error queue */ + XMEMSET(&client_args, 0, sizeof(func_args)); + XMEMSET(&server_args, 0, sizeof(func_args)); + + StartTCP(); + InitTcpReady(&ready); + + client_cb.method = wolfTLSv1_1_client_method; + server_cb.method = wolfTLSv1_2_server_method; + + server_args.signal = &ready; + server_args.callbacks = &server_cb; + client_args.signal = &ready; + client_args.callbacks = &client_cb; + + start_thread(test_server_nofail, &server_args, &serverThread); + wait_tcp_ready(&server_args); + test_client_nofail(&client_args); + join_thread(serverThread); + + FreeTcpReady(&ready); + /* check that error code was stored */ AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0); ERR_peek_last_error_line(NULL, &line); diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 6329f1e0b..91bdfaca1 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -570,6 +570,38 @@ int wc_SetLoggingHeap(void* h) return 0; } + +/* frees all nodes in the queue + * + * id this is the thread id + */ +int wc_ERR_remove_state(void) +{ + struct wc_error_queue* current; + struct wc_error_queue* next; + + if (wc_LockMutex(&debug_mutex) != 0) { + WOLFSSL_MSG("Lock debug mutex failed"); + return BAD_MUTEX_E; + } + + /* free all nodes from error queue */ + current = (struct wc_error_queue*)wc_errors; + while (current != NULL) { + next = current->next; + XFREE(current, current->heap, DYNAMIC_TYPE_LOG); + current = next; + } + + wc_errors = NULL; + wc_last_node = NULL; + + wc_UnLockMutex(&debug_mutex); + + return 0; +} + + #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) /* empties out the error queue into the file */ void wc_ERR_print_errors_fp(FILE* fp) diff --git a/wolfssl/wolfcrypt/logging.h b/wolfssl/wolfcrypt/logging.h index d60578ae7..154747d5a 100644 --- a/wolfssl/wolfcrypt/logging.h +++ b/wolfssl/wolfcrypt/logging.h @@ -64,6 +64,7 @@ WOLFSSL_API void wolfSSL_Debugging_OFF(void); WOLFSSL_LOCAL int wc_PullErrorNode(const char **file, const char **reason, int *line); WOLFSSL_API int wc_SetLoggingHeap(void* h); + WOLFSSL_API int wc_ERR_remove_state(void); #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) WOLFSSL_API void wc_ERR_print_errors_fp(FILE* fp); #endif