From 7f3c7497336d2791da11e69edac644943ebc4dce Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Mon, 30 May 2022 14:27:07 +0530 Subject: [PATCH] Added check to free keypair if already allocated to avoid memory leak --- nimble/host/src/ble_sm_alg.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nimble/host/src/ble_sm_alg.c b/nimble/host/src/ble_sm_alg.c index ee8b937c5..80165b892 100644 --- a/nimble/host/src/ble_sm_alg.c +++ b/nimble/host/src/ble_sm_alg.c @@ -628,6 +628,10 @@ mbedtls_gen_keypair(uint8_t *public_key, uint8_t *private_key) mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init(&ctr_drbg); + + /* Free the previously allocate keypair */ + mbedtls_ecp_keypair_free(&keypair); + mbedtls_ecp_keypair_init(&keypair); if (( rc = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, @@ -664,6 +668,11 @@ exit: return 0; } + +void mbedtls_free_keypair(void) +{ + mbedtls_ecp_keypair_free(&keypair); +} #endif /**