diff --git a/src/ssl.c b/src/ssl.c index 3f6f3a6eb..e98ae1cb6 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -20469,13 +20469,15 @@ unsigned long wolfSSL_ERR_peek_last_error_line(const char **file, int *line) (void)line; (void)file; #if defined(DEBUG_WOLFSSL) - if (line != NULL) { - *line = (int)wc_last_error_line; + { + int ret; + + if ((ret = wc_PeekErrorNode(-1, file, NULL, line)) < 0) { + WOLFSSL_MSG("Issue peeking at error node in queue"); + return 0; + } + return (unsigned long)ret; } - if (file != NULL) { - *file = (char*)wc_last_error_file; - } - return wc_last_error; #else return (unsigned long)(0 - NOT_COMPILED_IN); #endif diff --git a/tests/api.c b/tests/api.c index 0d5b1c205..d0d1a00d7 100644 --- a/tests/api.c +++ b/tests/api.c @@ -2703,7 +2703,6 @@ static void test_wolfSSL_ERR_peek_last_error_line(void) ERR_print_errors_fp(stdout); printf("Done testing print out\n\n"); fflush(stdout); - wolfSSL_Cleanup(); #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \ !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */ } diff --git a/wolfcrypt/src/logging.c b/wolfcrypt/src/logging.c index 56ce90fc1..1ecef97c0 100644 --- a/wolfcrypt/src/logging.c +++ b/wolfcrypt/src/logging.c @@ -273,7 +273,7 @@ void WOLFSSL_ERROR(int error) int wc_LoggingInit(void) { if (wc_InitMutex(&debug_mutex) != 0) { - WOLFSSL_MSG("Bad Init Mutex frnih"); + WOLFSSL_MSG("Bad Init Mutex"); return BAD_MUTEX_E; } XMEMSET((char*)wc_last_error_file, 0, sizeof(wc_last_error_file)); @@ -310,7 +310,7 @@ int wc_LoggingCleanup(void) wc_UnLockMutex(&debug_mutex); if (wc_FreeMutex(&debug_mutex) != 0) { - WOLFSSL_MSG("Bad Init Mutex frnih"); + WOLFSSL_MSG("Bad Mutex free"); return BAD_MUTEX_E; } return 0; @@ -318,6 +318,67 @@ int wc_LoggingCleanup(void) #ifdef DEBUG_WOLFSSL +/* peek at an error node + * + * index : if -1 then the most recent node is looked at, otherwise search + * through queue for node at the given index + * file : pointer to internal file string + * reason : pointer to internal error reason + * line : line number that error happened at + * + * Returns a negative value in error case, on success returns the nodes error + * value which is positve (absolute value) + */ +int wc_PeekErrorNode(int index, const char **file, const char **reason, + int *line) +{ + struct wc_error_queue* err; + + if (wc_LockMutex(&debug_mutex) != 0) { + WOLFSSL_MSG("Lock debug mutex failed"); + return BAD_MUTEX_E; + } + + if (index < 0) { + err = wc_last_node; + if (err == NULL) { + WOLFSSL_MSG("No Errors in queue"); + wc_UnLockMutex(&debug_mutex); + return BAD_STATE_E; + } + } + else { + int i; + + err = (struct wc_error_queue*)wc_errors; + for (i = 0; i < index; i++) { + if (err == NULL) { + WOLFSSL_MSG("Error node not found. Bad index?"); + wc_UnLockMutex(&debug_mutex); + return BAD_FUNC_ARG; + } + err = err->next; + } + } + + if (file != NULL) { + *file = err->file; + } + + if (reason != NULL) { + *reason = err->error; + } + + if (line != NULL) { + *line = err->line; + } + + wc_UnLockMutex(&debug_mutex); + + return err->value; +} + + /* create new error node and add it to the queue * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal * function */ diff --git a/wolfssl/wolfcrypt/logging.h b/wolfssl/wolfcrypt/logging.h index 1807e41df..e95e65835 100644 --- a/wolfssl/wolfcrypt/logging.h +++ b/wolfssl/wolfcrypt/logging.h @@ -60,6 +60,8 @@ WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); WOLFSSL_LOCAL int wc_LoggingCleanup(void); WOLFSSL_LOCAL int wc_AddErrorNode(int error, int line, char* buf, char* file); + WOLFSSL_LOCAL int wc_PeekErrorNode(int index, const char **file, + const char **reason, int *line); WOLFSSL_API int wc_SetLoggingHeap(void* h); #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) WOLFSSL_API void wc_ERR_print_errors_fp(FILE* fp);