diff --git a/src/internal.c b/src/internal.c index 7b9d2317a..001309442 100755 --- a/src/internal.c +++ b/src/internal.c @@ -1376,6 +1376,7 @@ int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap) if (InitMutex(&ctx->countMutex) < 0) { WOLFSSL_MSG("Mutex error on CTX init"); + ctx->err = (int)CTX_INIT_MUTEX_E; return BAD_MUTEX_E; } @@ -1537,6 +1538,13 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx) if (LockMutex(&ctx->countMutex) != 0) { WOLFSSL_MSG("Couldn't lock count mutex"); + + /* check error state, if mutex error code then mutex init failed but + * CTX was still malloc'd */ + if (ctx->err == (int)CTX_INIT_MUTEX_E) { + SSL_CtxResourceFree(ctx); + XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX); + } return; } ctx->refCount--; @@ -11344,6 +11352,9 @@ const char* wolfSSL_ERR_reason_error_string(unsigned long e) case INPUT_SIZE_E: return "Input size too large Error"; + case CTX_INIT_MUTEX_E: + return "Initialize ctx mutex error"; + default : return "unknown error number"; } diff --git a/wolfssl/error-ssl.h b/wolfssl/error-ssl.h index 933f7d0c9..4f484aa6b 100644 --- a/wolfssl/error-ssl.h +++ b/wolfssl/error-ssl.h @@ -149,6 +149,7 @@ enum wolfSSL_ErrorCodes { DTLS_EXPORT_VER_E = -411, /* export version error */ INPUT_SIZE_E = -412, /* input size too big error */ + CTX_INIT_MUTEX_E = -413, /* initialize ctx mutex error */ /* add strings to wolfSSL_ERR_reason_error_string in internal.c !!!!! */ /* begin negotiation parameter errors */ diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 01301001a..854d11433 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -1922,6 +1922,7 @@ struct WOLFSSL_CTX { WOLFSSL_METHOD* method; wolfSSL_Mutex countMutex; /* reference count mutex */ int refCount; /* reference count */ + int err; /* error code in case of mutex not created */ #ifndef NO_DH buffer serverDH_P; buffer serverDH_G;