Topic: Memory Leak When Freeing Context

I found a memory leak in wolfSSL. The memory leak is small, but impacted me since I'm working on a very resource constrained embedded ssl system.

Memory Leak:
struct CYASSL_CTX::countMutex is initialized but never freed

This mutex is initialized in InitSSL_Ctx() (internal.c) and should be freed in FreeSSL_Ctx() (also internal.c).

I have changed:

void FreeSSL_Ctx(CYASSL_CTX* ctx)
{
    int doFree = 0;

    if (LockMutex(&ctx->countMutex) != 0) {
        CYASSL_MSG("Couldn't lock count mutex");
        return;
    }
    ctx->refCount--;
    if (ctx->refCount == 0)
        doFree = 1;
    UnLockMutex(&ctx->countMutex);

    if (doFree) {
        CYASSL_MSG("CTX ref count down to 0, doing full free");
        SSL_CtxResourceFree(ctx);
        XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX);
    }
    else {
        (void)ctx;
        CYASSL_MSG("CTX ref count not 0 yet, no free");
    }
}

to

void FreeSSL_Ctx(CYASSL_CTX* ctx)
{
    int doFree = 0;

    if (LockMutex(&ctx->countMutex) != 0) {
        CYASSL_MSG("Couldn't lock count mutex");
        return;
    }
    ctx->refCount--;
    if (ctx->refCount == 0)
        doFree = 1;
    UnLockMutex(&ctx->countMutex);

    if (doFree) {
        CYASSL_MSG("CTX ref count down to 0, doing full free");
        SSL_CtxResourceFree(ctx);
        XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX);
        FreeMutex(&ctx->countMutex); //***** This Free was missing *****
    }
    else {
        (void)ctx;
        CYASSL_MSG("CTX ref count not 0 yet, no free");
    }
}

Wasn't sure how to submit bugs, but thought I would post it here in case anyone else out there is as resource constrained as I am.

Share

Re: Memory Leak When Freeing Context

Thanks for the report!

I just pushed a commit though a little different than yours.  The FreeMutex() needs to be one line up, using ctx->countMutex after freeing ctx will cause a problem on many systems.

Thanks again.

Share

Re: Memory Leak When Freeing Context

Indeed. Does need to be one line up.

Thanks

Share