Added new API wolfSSL_ResetAllocators to allow reset of memory callbacks to defaults. Added new CleanupMemoryTracker which restores memory callback functions. This resolves issue with trying to free memory allocated prior to InitMemoryTracker.

This commit is contained in:
David Garske
2018-07-23 16:00:03 -07:00
parent ab3ffaa26a
commit ef076a68d3
3 changed files with 20 additions and 4 deletions

View File

@ -46,9 +46,9 @@
/* Set these to default values initially. */ /* Set these to default values initially. */
static wolfSSL_Malloc_cb malloc_function = 0; static wolfSSL_Malloc_cb malloc_function = NULL;
static wolfSSL_Free_cb free_function = 0; static wolfSSL_Free_cb free_function = NULL;
static wolfSSL_Realloc_cb realloc_function = 0; static wolfSSL_Realloc_cb realloc_function = NULL;
int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf, int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
wolfSSL_Free_cb ff, wolfSSL_Free_cb ff,
@ -74,6 +74,16 @@ int wolfSSL_SetAllocators(wolfSSL_Malloc_cb mf,
return res; return res;
} }
int wolfSSL_ResetAllocators(void)
{
/* allow nulls to be set for callbacks to restore defaults */
malloc_function = NULL;
free_function = NULL;
realloc_function = NULL;
return 0;
}
int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf, int wolfSSL_GetAllocators(wolfSSL_Malloc_cb* mf,
wolfSSL_Free_cb* ff, wolfSSL_Free_cb* ff,
wolfSSL_Realloc_cb* rf) wolfSSL_Realloc_cb* rf)

View File

@ -237,6 +237,12 @@
(unsigned long)ourMemStats.currentBytes); (unsigned long)ourMemStats.currentBytes);
#endif #endif
} }
STATIC WC_INLINE int CleanupMemoryTracker(void)
{
/* restore default allocators */
return wolfSSL_ResetAllocators();
}
#endif #endif
#endif /* USE_WOLFSSL_MEMORY */ #endif /* USE_WOLFSSL_MEMORY */

View File

@ -77,7 +77,7 @@
WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb, WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
wolfSSL_Free_cb, wolfSSL_Free_cb,
wolfSSL_Realloc_cb); wolfSSL_Realloc_cb);
WOLFSSL_API int wolfSSL_ResetAllocators(void);
WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*, WOLFSSL_API int wolfSSL_GetAllocators(wolfSSL_Malloc_cb*,
wolfSSL_Free_cb*, wolfSSL_Free_cb*,
wolfSSL_Realloc_cb*); wolfSSL_Realloc_cb*);