Merge branch 'bugfix/btdm_fix_memory_leak_in_blufi' into 'master'

component/bt: fix memory leak in bluefi demo

See merge request !1436
This commit is contained in:
Jiang Jiang Jian 2017-11-01 11:01:14 +08:00
commit a8deadeba9
2 changed files with 21 additions and 6 deletions

View File

@ -184,11 +184,11 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
server_if=param->connect.server_if; server_if=param->connect.server_if;
conn_id=param->connect.conn_id; conn_id=param->connect.conn_id;
esp_ble_gap_stop_advertising(); esp_ble_gap_stop_advertising();
blufi_security_deinit();
blufi_security_init(); blufi_security_init();
break; break;
case ESP_BLUFI_EVENT_BLE_DISCONNECT: case ESP_BLUFI_EVENT_BLE_DISCONNECT:
BLUFI_INFO("BLUFI ble disconnect\n"); BLUFI_INFO("BLUFI ble disconnect\n");
blufi_security_deinit();
esp_ble_gap_start_advertising(&example_adv_params); esp_ble_gap_start_advertising(&example_adv_params);
break; break;
case ESP_BLUFI_EVENT_SET_WIFI_OPMODE: case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:

View File

@ -90,22 +90,28 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
blufi_sec->dh_param_len = ((data[1]<<8)|data[2]); blufi_sec->dh_param_len = ((data[1]<<8)|data[2]);
if (blufi_sec->dh_param) { if (blufi_sec->dh_param) {
free(blufi_sec->dh_param); free(blufi_sec->dh_param);
blufi_sec->dh_param = NULL;
} }
blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len); blufi_sec->dh_param = (uint8_t *)malloc(blufi_sec->dh_param_len);
if (blufi_sec->dh_param == NULL) { if (blufi_sec->dh_param == NULL) {
BLUFI_ERROR("%s, malloc failed\n", __func__);
return; return;
} }
break; break;
case SEC_TYPE_DH_PARAM_DATA: case SEC_TYPE_DH_PARAM_DATA:{
if (blufi_sec->dh_param == NULL) {
BLUFI_ERROR("%s, blufi_sec->dh_param == NULL\n", __func__);
return;
}
uint8_t *param = blufi_sec->dh_param;
memcpy(blufi_sec->dh_param, &data[1], blufi_sec->dh_param_len); memcpy(blufi_sec->dh_param, &data[1], blufi_sec->dh_param_len);
ret = mbedtls_dhm_read_params(&blufi_sec->dhm, &param, &param[blufi_sec->dh_param_len]);
ret = mbedtls_dhm_read_params(&blufi_sec->dhm, &blufi_sec->dh_param, &blufi_sec->dh_param[blufi_sec->dh_param_len]);
if (ret) { if (ret) {
BLUFI_ERROR("%s read param failed %d\n", __func__, ret); BLUFI_ERROR("%s read param failed %d\n", __func__, ret);
return; return;
} }
free(blufi_sec->dh_param);
blufi_sec->dh_param = NULL;
ret = mbedtls_dhm_make_public(&blufi_sec->dhm, (int) mbedtls_mpi_size( &blufi_sec->dhm.P ), blufi_sec->self_public_key, blufi_sec->dhm.len, myrand, NULL); ret = mbedtls_dhm_make_public(&blufi_sec->dhm, (int) mbedtls_mpi_size( &blufi_sec->dhm.P ), blufi_sec->self_public_key, blufi_sec->dhm.len, myrand, NULL);
if (ret) { if (ret) {
BLUFI_ERROR("%s make public failed %d\n", __func__, ret); BLUFI_ERROR("%s make public failed %d\n", __func__, ret);
@ -126,6 +132,8 @@ void blufi_dh_negotiate_data_handler(uint8_t *data, int len, uint8_t **output_da
*output_data = &blufi_sec->self_public_key[0]; *output_data = &blufi_sec->self_public_key[0];
*output_len = blufi_sec->dhm.len; *output_len = blufi_sec->dhm.len;
*need_free = false; *need_free = false;
}
break; break;
case SEC_TYPE_DH_P: case SEC_TYPE_DH_P:
break; break;
@ -194,6 +202,13 @@ esp_err_t blufi_security_init(void)
void blufi_security_deinit(void) void blufi_security_deinit(void)
{ {
if (blufi_sec == NULL) {
return;
}
if (blufi_sec->dh_param){
free(blufi_sec->dh_param);
blufi_sec->dh_param = NULL;
}
mbedtls_dhm_free(&blufi_sec->dhm); mbedtls_dhm_free(&blufi_sec->dhm);
mbedtls_aes_free(&blufi_sec->aes); mbedtls_aes_free(&blufi_sec->aes);