diff --git a/src/internal.c b/src/internal.c index cf2d88bee..a9c109dde 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1796,7 +1796,16 @@ void SSL_CtxResourceFree(WOLFSSL_CTX* ctx) wolfEventQueue_Free(&ctx->event_queue); #endif /* HAVE_WOLF_EVENT */ +#ifdef WOLFSSL_STATIC_MEMORY + if (ctx->onHeap == 1) { + XFREE(ctx->method, ctx->heap, DYNAMIC_TYPE_METHOD); + } + else { + XFREE(ctx->method, NULL, DYNAMIC_TYPE_METHOD); + } +#else XFREE(ctx->method, ctx->heap, DYNAMIC_TYPE_METHOD); +#endif ctx->method = NULL; if (ctx->suites) { XFREE(ctx->suites, ctx->heap, DYNAMIC_TYPE_SUITES); @@ -1920,10 +1929,17 @@ void FreeSSL_Ctx(WOLFSSL_CTX* ctx) wc_UnLockMutex(&ctx->countMutex); if (doFree) { + void* heap = ctx->heap; WOLFSSL_MSG("CTX ref count down to 0, doing full free"); SSL_CtxResourceFree(ctx); wc_FreeMutex(&ctx->countMutex); - XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX); +#ifdef WOLFSSL_STATIC_MEMORY + if (ctx->onHeap == 0) { + heap = NULL; + } +#endif + XFREE(ctx, heap, DYNAMIC_TYPE_CTX); + (void)heap; /* not used in some builds */ } else { (void)ctx; diff --git a/src/ssl.c b/src/ssl.c index 77fd26684..d4067db5d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -1398,6 +1398,7 @@ int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method WOLFSSL_MSG("Error creating ctx"); return WOLFSSL_FAILURE; } + (*ctx)->onHeap = 1; /* free the memory back to heap when done */ } /* determine what max applies too */ diff --git a/wolfssl/internal.h b/wolfssl/internal.h index c2cc5e97f..5a9d6cd3a 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -2617,6 +2617,9 @@ struct WOLFSSL_CTX { #ifdef HAVE_ENCRYPT_THEN_MAC byte disallowEncThenMac:1; /* Don't do Encrypt-Then-MAC */ #endif +#ifdef WOLFSSL_STATIC_MEMORY + byte onHeap:1; /* whether the ctx/method is put on heap hint */ +#endif #ifdef WOLFSSL_MULTICAST byte haveMcast; /* multicast requested */ byte mcastID; /* multicast group ID */