@@ -5930,13 +5930,6 @@ void InitX509(WOLFSSL_X509* x509, int dynamicFlag, void* heap)
59305930 (void)ret;
59315931 }
59325932#endif
5933- #ifdef WOLFSSL_STATIC_MEMORY
5934- /* A dynamic X509 can outlive the SSL whose heap hint it uses; dropped in
5935- * ExternalFreeX509() */
5936- if (dynamicFlag) {
5937- wc_HeapHint_Ref(heap);
5938- }
5939- #endif
59405933}
59415934
59425935
@@ -8893,6 +8886,7 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
88938886 if (ctx->heap != NULL) {
88948887 WOLFSSL_HEAP_HINT* ssl_hint;
88958888 WOLFSSL_HEAP_HINT* ctx_hint;
8889+ int ret = 0;
88968890
88978891 /* avoid dereferencing a test value */
88988892 #ifdef WOLFSSL_HEAP_TEST
@@ -8911,13 +8905,24 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
89118905 ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap));
89128906
89138907 ssl_hint->memory = ctx_hint->memory;
8914- ssl_hint->refCount = 1; /* the SSL's own reference */
8908+ /* the SSL's ref; each allocated block holds another */
8909+ wolfSSL_RefInit(&ssl_hint->refCount, &ret);
8910+ #ifdef WOLFSSL_REFCNT_ERROR_RETURN
8911+ if (ret != 0) {
8912+ WOLFSSL_MSG("Error setting up heap hint reference mutex");
8913+ XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
8914+ ssl->heap = NULL;
8915+ return ret;
8916+ }
8917+ #endif
8918+ (void)ret;
89158919 ssl_hint->sslOwned = 1;
89168920 #ifndef WOLFSSL_STATIC_MEMORY_LEAN
89178921 #ifndef SINGLE_THREADED
89188922 /* lock and check IO count / handshake count */
89198923 if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
89208924 WOLFSSL_MSG("Bad memory_mutex lock");
8925+ wolfSSL_RefFree(&ssl_hint->refCount);
89218926 XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
89228927 ssl->heap = NULL; /* free and set to NULL for IO counter */
89238928 WOLFSSL_ERROR_VERBOSE(BAD_MUTEX_E);
@@ -8930,6 +8935,7 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
89308935 #ifndef SINGLE_THREADED
89318936 wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
89328937 #endif
8938+ wolfSSL_RefFree(&ssl_hint->refCount);
89338939 XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
89348940 ssl->heap = NULL; /* free and set to NULL for IO counter */
89358941 return MEMORY_E;
@@ -8941,6 +8947,7 @@ static int InitSSL_StaticMemory(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
89418947 #ifndef SINGLE_THREADED
89428948 wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
89438949 #endif
8950+ wolfSSL_RefFree(&ssl_hint->refCount);
89448951 XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
89458952 ssl->heap = NULL; /* free and set to NULL for IO counter */
89468953 return MEMORY_E;
@@ -10303,8 +10310,7 @@ static void FreeSSL_StaticMemory(WOLFSSL* ssl)
1030310310 WOLFSSL_MSG("Bad memory_mutex lock");
1030410311 }
1030510312 #endif
10306- /* detach stats under the lock: sessions and X509s holding a reference
10307- * may still allocate through this hint from other threads */
10313+ /* detach under the lock: live blocks may still use the hint */
1030810314 if (ctx_heap->flag & WOLFMEM_TRACK_STATS) {
1030910315 stats = ssl_hint->stats;
1031010316 ssl_hint->stats = NULL;
@@ -10328,8 +10334,8 @@ static void FreeSSL_StaticMemory(WOLFSSL* ssl)
1032810334 * ssl->heap, which the SSL's reference still keeps alive here */
1032910335 XFREE(stats, heap, DYNAMIC_TYPE_SSL);
1033010336 #endif
10331- /* drop the SSL's reference; other holders may keep the hint alive */
10332- wc_HeapHint_Unref (ssl->heap);
10337+ /* drop the SSL's ref; the last block's free may free the hint */
10338+ wc_HeapHint_Release (ssl->heap);
1033310339 ssl->heap = NULL;
1033410340 #ifdef WOLFSSL_HEAP_TEST
1033510341 }
0 commit comments