From 91b7cddb7c619c719ecd746e48d6fa77fbd77e2c Mon Sep 17 00:00:00 2001 From: toddouska Date: Mon, 23 Nov 2015 15:13:36 -0800 Subject: [PATCH] better error checking on condition variable operations, cleanup --- src/crl.c | 47 +++++++++++++++++++++++++++------ wolfcrypt/src/error.c | 3 +++ wolfssl/wolfcrypt/error-crypt.h | 1 + 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/crl.c b/src/crl.c index 244a686f7..03515bd3d 100644 --- a/src/crl.c +++ b/src/crl.c @@ -58,10 +58,15 @@ int InitCRL(WOLFSSL_CRL* crl, WOLFSSL_CERT_MANAGER* cm) crl->tid = 0; crl->mfd = -1; /* mfd for bsd is kqueue fd, eventfd for linux */ crl->setup = 0; /* thread setup done predicate */ - pthread_cond_init(&crl->cond, 0); + if (pthread_cond_init(&crl->cond, 0) != 0) { + WOLFSSL_MSG("Pthread condition init failed"); + return BAD_COND_E; + } #endif - if (InitMutex(&crl->crlLock) != 0) - return BAD_MUTEX_E; + if (InitMutex(&crl->crlLock) != 0) { + WOLFSSL_MSG("Init Mutex failed"); + return BAD_MUTEX_E; + } return 0; } @@ -329,6 +334,8 @@ int BufferLoadCRL(WOLFSSL_CRL* crl, const byte* buff, long sz, int type) /* Signal Monitor thread is setup, save status to setup flag, 0 on success */ static int SignalSetup(WOLFSSL_CRL* crl, int status) { + int ret; + /* signal to calling thread we're setup */ if (LockMutex(&crl->crlLock) != 0) { WOLFSSL_MSG("LockMutex crlLock failed"); @@ -336,10 +343,13 @@ static int SignalSetup(WOLFSSL_CRL* crl, int status) } crl->setup = status; - pthread_cond_signal(&crl->cond); + ret = pthread_cond_signal(&crl->cond); UnLockMutex(&crl->crlLock); + if (ret != 0) + return BAD_COND_E; + return 0; } @@ -501,6 +511,8 @@ static void* DoMonitor(void* arg) fDER = open(crl->monitors[1].path, XEVENT_MODE); if (fDER == -1) { WOLFSSL_MSG("DER event dir open failed"); + if (fPEM != -1) + close(fPEM); close(crl->mfd); SignalSetup(crl, MONITOR_SETUP_E); return NULL; @@ -516,8 +528,14 @@ static void* DoMonitor(void* arg) NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0); /* signal to calling thread we're setup */ - if (SignalSetup(crl, 1) != 0) + if (SignalSetup(crl, 1) != 0) { + if (fPEM != -1) + close(fPEM); + if (fDER != -1) + close(fDER); + close(crl->mfd); return NULL; + } for (;;) { struct kevent event; @@ -642,8 +660,17 @@ static void* DoMonitor(void* arg) #endif /* signal to calling thread we're setup */ - if (SignalSetup(crl, 1) != 0) + if (SignalSetup(crl, 1) != 0) { + #ifdef WOLFSSL_SMALL_STACK + XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER); + #endif + + if (wd > 0) + inotify_rm_watch(notifyFd, wd); + close(crl->mfd); + close(notifyFd); return NULL; + } for (;;) { fd_set readfds; @@ -725,8 +752,12 @@ static int StartMonitorCRL(WOLFSSL_CRL* crl) return BAD_MUTEX_E; } - while (crl->setup == 0) - pthread_cond_wait(&crl->cond, &crl->crlLock); + while (crl->setup == 0) { + if (pthread_cond_wait(&crl->cond, &crl->crlLock) != 0) { + ret = BAD_COND_E; + break; + } + } if (crl->setup < 0) ret = crl->setup; /* store setup error */ diff --git a/wolfcrypt/src/error.c b/wolfcrypt/src/error.c index 36271c3cc..dd570a31a 100644 --- a/wolfcrypt/src/error.c +++ b/wolfcrypt/src/error.c @@ -364,6 +364,9 @@ const char* wc_GetErrorString(int error) case SIG_VERIFY_E: return "Signature verify error"; + case BAD_COND_E: + return "Bad condition variable operation error"; + default: return "unknown error number"; diff --git a/wolfssl/wolfcrypt/error-crypt.h b/wolfssl/wolfcrypt/error-crypt.h index adf2d96b3..187ef324a 100644 --- a/wolfssl/wolfcrypt/error-crypt.h +++ b/wolfssl/wolfcrypt/error-crypt.h @@ -163,6 +163,7 @@ enum { WC_INIT_E = -228, /* wolfcrypt failed to initialize */ SIG_VERIFY_E = -229, /* wolfcrypt signature verify error */ + BAD_COND_E = -230, /* Bad condition variable operation */ MIN_CODE_E = -300 /* errors -101 - -299 */ };