diff --git a/components/bt/common/btc/profile/esp/blufi/blufi_prf.c b/components/bt/common/btc/profile/esp/blufi/blufi_prf.c index 40b627c4d0b..7a73ac4976f 100644 --- a/components/bt/common/btc/profile/esp/blufi/blufi_prf.c +++ b/components/bt/common/btc/profile/esp/blufi/blufi_prf.c @@ -281,6 +281,7 @@ static void btc_blufi_wifi_conn_report(uint8_t opmode, uint8_t sta_conn_state, u data_len = info_len + 3; p = data = osi_malloc(data_len); if (data == NULL) { + BTC_TRACE_ERROR("%s no mem\n", __func__); return; } @@ -413,6 +414,7 @@ static void btc_blufi_send_error_info(uint8_t state) data_len = 1; p = data = osi_malloc(data_len); if (data == NULL) { + BTC_TRACE_ERROR("%s no mem\n", __func__); return; } @@ -688,6 +690,7 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) dst->wifi_conn_report.extra_info = osi_calloc(sizeof(esp_blufi_extra_info_t)); if (dst->wifi_conn_report.extra_info == NULL) { + BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__); return; } @@ -702,6 +705,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) memcpy(dst->wifi_conn_report.extra_info->sta_ssid, src_info->sta_ssid, src_info->sta_ssid_len); dst->wifi_conn_report.extra_info->sta_ssid_len = src_info->sta_ssid_len; dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_ssid_len + 2); + } else { + BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__); + return; } } if (src_info->sta_passwd) { @@ -710,6 +716,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) memcpy(dst->wifi_conn_report.extra_info->sta_passwd, src_info->sta_passwd, src_info->sta_passwd_len); dst->wifi_conn_report.extra_info->sta_passwd_len = src_info->sta_passwd_len; dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->sta_passwd_len + 2); + } else { + BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__); + return; } } if (src_info->softap_ssid) { @@ -718,6 +727,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) memcpy(dst->wifi_conn_report.extra_info->softap_ssid, src_info->softap_ssid, src_info->softap_ssid_len); dst->wifi_conn_report.extra_info->softap_ssid_len = src_info->softap_ssid_len; dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_ssid_len + 2); + } else { + BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__); + return; } } if (src_info->softap_passwd) { @@ -726,6 +738,9 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) memcpy(dst->wifi_conn_report.extra_info->softap_passwd, src_info->softap_passwd, src_info->softap_passwd_len); dst->wifi_conn_report.extra_info->softap_passwd_len = src_info->softap_passwd_len; dst->wifi_conn_report.extra_info_len += (dst->wifi_conn_report.extra_info->softap_passwd_len + 2); + } else { + BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__); + return; } } if (src_info->softap_authmode_set) { @@ -768,6 +783,7 @@ void btc_blufi_call_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src) } dst->wifi_list.list = (esp_blufi_ap_record_t *)osi_malloc(sizeof(esp_blufi_ap_record_t) * src->wifi_list.apCount); if (dst->wifi_list.list == NULL) { + BTC_TRACE_ERROR("%s no mem line %d\n", __func__, __LINE__); break; } memcpy(dst->wifi_list.list, list, sizeof(esp_blufi_ap_record_t) * src->wifi_list.apCount); diff --git a/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c b/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c index fa1464bd019..6a3def567b8 100644 --- a/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c +++ b/examples/bluetooth/bluedroid/ble/ble_ancs/main/ble_ancs_demo.c @@ -384,13 +384,15 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (ret_status != ESP_GATT_OK) { ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__); + break; } if (count > 0) { char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); - memset(char_elem_result, 0xff, sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result) { ESP_LOGE(BLE_ANCS_TAG, "gattc no mem"); + break; } else { + memset(char_elem_result, 0xff, sizeof(esp_gattc_char_elem_t) * count); ret_status = esp_ble_gattc_get_all_char(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, gl_profile_tab[PROFILE_A_APP_ID].service_start_handle, @@ -400,6 +402,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ offset); if (ret_status != ESP_GATT_OK) { ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_all_char error, %d", __LINE__); + free(char_elem_result); + char_elem_result = NULL; + break; } if (count > 0) { @@ -432,6 +437,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } free(char_elem_result); + char_elem_result = NULL; } } else { ESP_LOGE(BLE_ANCS_TAG, "No Apple Notification Service found"); @@ -456,11 +462,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (ret_status != ESP_GATT_OK) { ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__); + break; } if (count > 0) { descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result) { ESP_LOGE(BLE_ANCS_TAG, "malloc error, gattc no mem"); + break; } else { ret_status = esp_ble_gattc_get_all_descr(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, @@ -470,6 +478,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ offset); if (ret_status != ESP_GATT_OK) { ESP_LOGE(BLE_ANCS_TAG, "esp_ble_gattc_get_all_descr error, %d", __LINE__); + free(descr_elem_result); + descr_elem_result = NULL; + break; } for (int i = 0; i < count; ++ i) { @@ -487,6 +498,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } free(descr_elem_result); + descr_elem_result = NULL; } break; } diff --git a/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c b/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c index f5bfd969a86..d875471a811 100644 --- a/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c +++ b/examples/bluetooth/bluedroid/ble/ble_compatibility_test/main/ble_compatibility_test.c @@ -248,6 +248,10 @@ static void show_bonded_devices(void) int dev_num = esp_ble_get_bond_device_num(); esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + if (!dev_list) { + ESP_LOGE(EXAMPLE_TAG, "malloc failed, return\n"); + return; + } esp_ble_get_bond_device_list(&dev_num, dev_list); EXAMPLE_DEBUG(EXAMPLE_TAG, "Bonded devices number : %d\n", dev_num); @@ -266,6 +270,10 @@ static void __attribute__((unused)) remove_all_bonded_devices(void) int dev_num = esp_ble_get_bond_device_num(); esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + if (!dev_list) { + ESP_LOGE(EXAMPLE_TAG, "malloc failed, return\n"); + return; + } esp_ble_get_bond_device_list(&dev_num, dev_list); for (int i = 0; i < dev_num; i++) { esp_ble_remove_bond_device(dev_list[i].bd_addr); @@ -414,7 +422,8 @@ void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t } free(gatt_rsp); }else{ - ESP_LOGE(EXAMPLE_TAG, "%s, malloc failed", __func__); + ESP_LOGE(EXAMPLE_TAG, "%s, malloc failed, and no resource to send response", __func__); + status = ESP_GATT_NO_RESOURCES; } } if (status != ESP_GATT_OK){ diff --git a/examples/bluetooth/bluedroid/ble/ble_spp_server/main/ble_spp_server_demo.c b/examples/bluetooth/bluedroid/ble/ble_spp_server/main/ble_spp_server_demo.c index f1688acd8c9..72950bf033f 100644 --- a/examples/bluetooth/bluedroid/ble/ble_spp_server/main/ble_spp_server_demo.c +++ b/examples/bluetooth/bluedroid/ble/ble_spp_server/main/ble_spp_server_demo.c @@ -271,7 +271,13 @@ static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data) temp_spp_recv_data_node_p1->next_node = NULL; temp_spp_recv_data_node_p1->node_buff = (uint8_t *)malloc(p_data->write.len); temp_spp_recv_data_node_p2 = temp_spp_recv_data_node_p1; - memcpy(temp_spp_recv_data_node_p1->node_buff,p_data->write.value,p_data->write.len); + if (temp_spp_recv_data_node_p1->node_buff == NULL) { + ESP_LOGI(GATTS_TABLE_TAG, "malloc error %s %d\n", __func__, __LINE__); + temp_spp_recv_data_node_p1->len = 0; + } else { + memcpy(temp_spp_recv_data_node_p1->node_buff,p_data->write.value,p_data->write.len); + } + if(SppRecvDataBuff.node_num == 0){ SppRecvDataBuff.first_node = temp_spp_recv_data_node_p1; SppRecvDataBuff.node_num++; @@ -288,7 +294,9 @@ static void free_write_buffer(void) while(temp_spp_recv_data_node_p1 != NULL){ temp_spp_recv_data_node_p2 = temp_spp_recv_data_node_p1->next_node; - free(temp_spp_recv_data_node_p1->node_buff); + if (temp_spp_recv_data_node_p1->node_buff) { + free(temp_spp_recv_data_node_p1->node_buff); + } free(temp_spp_recv_data_node_p1); temp_spp_recv_data_node_p1 = temp_spp_recv_data_node_p2; } diff --git a/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c b/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c index 9418893bb9c..e64f4a6f407 100644 --- a/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c +++ b/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_client/main/example_ble_client_throughput.c @@ -205,12 +205,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); + break; } if (count > 0){ char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else{ status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, @@ -219,8 +221,11 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ remote_filter_char_uuid, char_elem_result, &count); - if (status != ESP_GATT_OK){ + if (status != ESP_GATT_OK) { ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); + free(char_elem_result); + char_elem_result = NULL; + break; } /* Every service has only one char in our 'throughput_server' demo, so we use first 'char_elem_result' */ @@ -231,6 +236,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } /* free char_elem_result */ free(char_elem_result); + char_elem_result = NULL; }else{ ESP_LOGE(GATTC_TAG, "no char found"); } diff --git a/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_server/main/example_ble_server_throughput.c b/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_server/main/example_ble_server_throughput.c index cfdd323ee2b..8ce8d265734 100644 --- a/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_server/main/example_ble_server_throughput.c +++ b/examples/bluetooth/bluedroid/ble/ble_throughput/throughput_server/main/example_ble_server_throughput.c @@ -297,17 +297,23 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare } esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t)); - gatt_rsp->attr_value.len = param->write.len; - gatt_rsp->attr_value.handle = param->write.handle; - gatt_rsp->attr_value.offset = param->write.offset; - gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; - memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); - esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); - - if (response_err != ESP_OK) { - ESP_LOGE(GATTS_TAG, "Send response error"); + if (gatt_rsp) { + gatt_rsp->attr_value.len = param->write.len; + gatt_rsp->attr_value.handle = param->write.handle; + gatt_rsp->attr_value.offset = param->write.offset; + gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; + memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); + esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); + + if (response_err != ESP_OK) { + ESP_LOGE(GATTS_TAG, "Send response error\n"); + } + free(gatt_rsp); + } else { + ESP_LOGE(GATTS_TAG, "malloc failed, no resource to send response error\n"); + status = ESP_GATT_NO_RESOURCES; } - free(gatt_rsp); + if (status != ESP_GATT_OK) { return; } diff --git a/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c b/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c index 277fcffb41d..5f1d10f1f7e 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_client/main/gattc_demo.c @@ -174,12 +174,14 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); + break; } if (count > 0){ char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else{ status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, @@ -190,6 +192,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); + free(char_elem_result); + char_elem_result = NULL; + break; } /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ @@ -221,11 +226,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); + break; } if (count > 0){ descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); + break; }else{ ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, @@ -235,6 +242,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); + free(descr_elem_result); + descr_elem_result = NULL; + break; } /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG){ diff --git a/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c b/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c index ba3f757e002..f5e60c0939b 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_security_client/main/example_ble_sec_gattc_demo.c @@ -227,11 +227,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__); + break; } if (count > 0){ char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else{ ret_status = esp_ble_gattc_get_all_char(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, @@ -242,6 +244,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ offset); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_all_char error, %d", __LINE__); + free(char_elem_result); + char_elem_result = NULL; + break; } if (count > 0){ @@ -259,6 +264,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } free(char_elem_result); + char_elem_result = NULL; } } @@ -281,11 +287,13 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error, %d", __LINE__); + break; } if (count > 0){ descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); + break; }else{ ret_status = esp_ble_gattc_get_all_descr(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, @@ -295,6 +303,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ offset); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_all_descr error, %d", __LINE__); + free(descr_elem_result); + descr_elem_result = NULL; + break; } for (int i = 0; i < count; ++i) @@ -314,6 +325,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } free(descr_elem_result); + descr_elem_result = NULL; } break; diff --git a/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c b/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c index f36548d2817..8324d170ea4 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_security_server/main/example_ble_sec_gatts_demo.c @@ -258,6 +258,10 @@ static void show_bonded_devices(void) int dev_num = esp_ble_get_bond_device_num(); esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + if (!dev_list) { + ESP_LOGI(GATTS_TABLE_TAG, "malloc failed, return\n"); + return; + } esp_ble_get_bond_device_list(&dev_num, dev_list); ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices number : %d", dev_num); @@ -274,6 +278,10 @@ static void __attribute__((unused)) remove_all_bonded_devices(void) int dev_num = esp_ble_get_bond_device_num(); esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + if (!dev_list) { + ESP_LOGI(GATTS_TABLE_TAG, "malloc failed, return\n"); + return; + } esp_ble_get_bond_device_list(&dev_num, dev_list); for (int i = 0; i < dev_num; i++) { esp_ble_remove_bond_device(dev_list[i].bd_addr); diff --git a/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c b/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c index f139e868608..91e495de8d3 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_server/main/gatts_demo.c @@ -258,16 +258,21 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare } esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t)); - gatt_rsp->attr_value.len = param->write.len; - gatt_rsp->attr_value.handle = param->write.handle; - gatt_rsp->attr_value.offset = param->write.offset; - gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; - memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); - esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); - if (response_err != ESP_OK){ - ESP_LOGE(GATTS_TAG, "Send response error"); + if (gatt_rsp) { + gatt_rsp->attr_value.len = param->write.len; + gatt_rsp->attr_value.handle = param->write.handle; + gatt_rsp->attr_value.offset = param->write.offset; + gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; + memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); + esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); + if (response_err != ESP_OK){ + ESP_LOGE(GATTS_TAG, "Send response error\n"); + } + free(gatt_rsp); + } else { + ESP_LOGE(GATTS_TAG, "malloc failed, no resource to send response error\n"); + status = ESP_GATT_NO_RESOURCES; } - free(gatt_rsp); if (status != ESP_GATT_OK){ return; } diff --git a/examples/bluetooth/bluedroid/ble/gatt_server_service_table/main/gatts_table_creat_demo.c b/examples/bluetooth/bluedroid/ble/gatt_server_service_table/main/gatts_table_creat_demo.c index 0110d3b2388..270153901d7 100644 --- a/examples/bluetooth/bluedroid/ble/gatt_server_service_table/main/gatts_table_creat_demo.c +++ b/examples/bluetooth/bluedroid/ble/gatt_server_service_table/main/gatts_table_creat_demo.c @@ -311,6 +311,7 @@ void example_prepare_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t free(gatt_rsp); }else{ ESP_LOGE(GATTS_TABLE_TAG, "%s, malloc failed", __func__); + status = ESP_GATT_NO_RESOURCES; } } if (status != ESP_GATT_OK){ diff --git a/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c b/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c index 25330a99886..e09ca88cbbf 100644 --- a/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c +++ b/examples/bluetooth/bluedroid/ble/gattc_multi_connect/main/gattc_multi_connect.c @@ -201,11 +201,13 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_attr_count error"); + break; } if (count > 0) { char_elem_result_a = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result_a){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else { status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, @@ -216,6 +218,9 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); + free(char_elem_result_a); + char_elem_result_a = NULL; + break; } /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ @@ -226,6 +231,7 @@ static void gattc_profile_a_event_handler(esp_gattc_cb_event_t event, esp_gatt_i } /* free char_elem_result */ free(char_elem_result_a); + char_elem_result_a = NULL; }else { ESP_LOGE(GATTC_TAG, "no char found"); } @@ -407,6 +413,7 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i char_elem_result_b = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result_b){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else{ status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, @@ -417,6 +424,9 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); + free(char_elem_result_b); + char_elem_result_b = NULL; + break; } /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ @@ -427,6 +437,7 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i } /* free char_elem_result */ free(char_elem_result_b); + char_elem_result_b = NULL; }else{ ESP_LOGE(GATTC_TAG, "no char found"); } @@ -463,6 +474,9 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); + free(descr_elem_result_b); + descr_elem_result_b = NULL; + break; } /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ @@ -482,6 +496,7 @@ static void gattc_profile_b_event_handler(esp_gattc_cb_event_t event, esp_gatt_i /* free descr_elem_result */ free(descr_elem_result_b); + descr_elem_result_b = NULL; } } else{ @@ -606,6 +621,7 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i char_elem_result_c = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result_c){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else{ status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, @@ -616,6 +632,9 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i &count); if (status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_char_by_uuid error"); + free(char_elem_result_c); + char_elem_result_c = NULL; + break; } /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ @@ -626,6 +645,7 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i } /* free char_elem_result */ free(char_elem_result_c); + char_elem_result_c = NULL; }else{ ESP_LOGE(GATTC_TAG, "no char found"); } @@ -652,6 +672,7 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i descr_elem_result_c = (esp_gattc_descr_elem_t *)malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result_c){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); + break; }else{ ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if, gl_profile_tab[PROFILE_C_APP_ID].conn_id, @@ -661,6 +682,9 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i &count); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); + free(descr_elem_result_c); + descr_elem_result_c = NULL; + break; } /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ @@ -680,6 +704,7 @@ static void gattc_profile_c_event_handler(esp_gattc_cb_event_t event, esp_gatt_i /* free descr_elem_result */ free(descr_elem_result_c); + descr_elem_result_c = NULL; } } else{ diff --git a/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c b/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c index 8d805bec9fe..f4a66878a5a 100644 --- a/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c +++ b/examples/bluetooth/bluedroid/ble_50/ble50_security_client/main/ble50_sec_gattc_demo.c @@ -275,6 +275,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result){ ESP_LOGE(GATTC_TAG, "gattc no mem"); + break; }else{ ret_status = esp_ble_gattc_get_all_char(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, @@ -285,6 +286,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ offset); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_all_char error, %d", __LINE__); + free(char_elem_result); + char_elem_result = NULL; + break; } if (count > 0){ @@ -302,6 +306,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } free(char_elem_result); + char_elem_result = NULL; } } @@ -329,6 +334,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result){ ESP_LOGE(GATTC_TAG, "malloc error, gattc no mem"); + break; }else{ ret_status = esp_ble_gattc_get_all_descr(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].conn_id, @@ -338,6 +344,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ offset); if (ret_status != ESP_GATT_OK){ ESP_LOGE(GATTC_TAG, "esp_ble_gattc_get_all_descr error, %d", __LINE__); + free(descr_elem_result); + descr_elem_result = NULL; + break; } for (int i = 0; i < count; ++i) @@ -357,6 +366,7 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } } free(descr_elem_result); + descr_elem_result = NULL; } break; diff --git a/examples/bluetooth/bluedroid/ble_50/ble50_security_server/main/ble50_sec_gatts_demo.c b/examples/bluetooth/bluedroid/ble_50/ble50_security_server/main/ble50_sec_gatts_demo.c index e494d510240..32264b02343 100644 --- a/examples/bluetooth/bluedroid/ble_50/ble50_security_server/main/ble50_sec_gatts_demo.c +++ b/examples/bluetooth/bluedroid/ble_50/ble50_security_server/main/ble50_sec_gatts_demo.c @@ -230,6 +230,10 @@ static void show_bonded_devices(void) int dev_num = esp_ble_get_bond_device_num(); esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + if (!dev_list) { + ESP_LOGE(GATTS_TABLE_TAG, "malloc failed\n"); + return; + } esp_ble_get_bond_device_list(&dev_num, dev_list); ESP_LOGI(GATTS_TABLE_TAG, "Bonded devices number : %d", dev_num); for (int i = 0; i < dev_num; i++) { @@ -244,6 +248,10 @@ static void __attribute__((unused)) remove_all_bonded_devices(void) int dev_num = esp_ble_get_bond_device_num(); esp_ble_bond_dev_t *dev_list = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * dev_num); + if (!dev_list) { + ESP_LOGE(GATTS_TABLE_TAG, "malloc failed\n"); + return; + } esp_ble_get_bond_device_list(&dev_num, dev_list); for (int i = 0; i < dev_num; i++) { esp_ble_remove_bond_device(dev_list[i].bd_addr); diff --git a/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c b/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c index 54ad6654e0a..166435aecb2 100644 --- a/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c +++ b/examples/bluetooth/bluedroid/coex/a2dp_gatts_coex/main/main.c @@ -214,16 +214,21 @@ void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t *prepare } esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t)); - gatt_rsp->attr_value.len = param->write.len; - gatt_rsp->attr_value.handle = param->write.handle; - gatt_rsp->attr_value.offset = param->write.offset; - gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; - memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); - esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); - if (response_err != ESP_OK){ - ESP_LOGE(BT_BLE_COEX_TAG, "Send response error"); + if (gatt_rsp) { + gatt_rsp->attr_value.len = param->write.len; + gatt_rsp->attr_value.handle = param->write.handle; + gatt_rsp->attr_value.offset = param->write.offset; + gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; + memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); + esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); + if (response_err != ESP_OK){ + ESP_LOGE(BT_BLE_COEX_TAG, "Send response error\n"); + } + free(gatt_rsp); + } else { + ESP_LOGE(BT_BLE_COEX_TAG, "%s, malloc failed", __func__); + status = ESP_GATT_NO_RESOURCES; } - free(gatt_rsp); if (status != ESP_GATT_OK){ return; } diff --git a/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c b/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c index ee032531d37..724b2a242cc 100644 --- a/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c +++ b/examples/bluetooth/bluedroid/coex/gattc_gatts_coex/main/gattc_gatts_coex.c @@ -388,7 +388,8 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ if (count > 0) { char_elem_result = (esp_gattc_char_elem_t *)malloc(sizeof(esp_gattc_char_elem_t) * count); if (!char_elem_result) { - ESP_LOGE(COEX_TAG, "gattc no mem"); + ESP_LOGE(COEX_TAG, "gattc no mem\n"); + break; }else { status = esp_ble_gattc_get_char_by_uuid( gattc_if, p_data->search_cmpl.conn_id, @@ -398,7 +399,10 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ char_elem_result, &count); if (status != ESP_GATT_OK) { - ESP_LOGE(COEX_TAG, "esp_ble_gattc_get_char_by_uuid error"); + ESP_LOGE(COEX_TAG, "esp_ble_gattc_get_char_by_uuid error\n"); + free(char_elem_result); + char_elem_result = NULL; + break; } /* Every service have only one char in our 'ESP_GATTS_DEMO' demo, so we used first 'char_elem_result' */ @@ -409,8 +413,9 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } /* free char_elem_result */ free(char_elem_result); + char_elem_result = NULL; } else { - ESP_LOGE(COEX_TAG, "no char found"); + ESP_LOGE(COEX_TAG, "no char found\n"); } } break; @@ -434,7 +439,8 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ if (count > 0) { descr_elem_result = malloc(sizeof(esp_gattc_descr_elem_t) * count); if (!descr_elem_result) { - ESP_LOGE(COEX_TAG, "malloc error, gattc no mem"); + ESP_LOGE(COEX_TAG, "malloc error, gattc no mem\n"); + break; } else { ret_status = esp_ble_gattc_get_descr_by_char_handle( gattc_if, gattc_profile_tab[GATTC_PROFILE_C_APP_ID].conn_id, @@ -443,7 +449,10 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ descr_elem_result, &count); if (ret_status != ESP_GATT_OK) { - ESP_LOGE(COEX_TAG, "esp_ble_gattc_get_descr_by_char_handle error"); + ESP_LOGE(COEX_TAG, "esp_ble_gattc_get_descr_by_char_handle error\n"); + free(descr_elem_result); + descr_elem_result = NULL; + break; } /* Every char has only one descriptor in our 'ESP_GATTS_DEMO' demo, so we used first 'descr_elem_result' */ if (count > 0 && descr_elem_result[0].uuid.len == ESP_UUID_LEN_16 && descr_elem_result[0].uuid.uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG) { @@ -462,9 +471,10 @@ static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ /* free descr_elem_result */ free(descr_elem_result); + descr_elem_result = NULL; } } else { - ESP_LOGE(COEX_TAG, "decsr not found"); + ESP_LOGE(COEX_TAG, "decsr not found\n"); } } @@ -541,16 +551,21 @@ static void example_write_event_env(esp_gatt_if_t gatts_if, prepare_type_env_t * } esp_gatt_rsp_t *gatt_rsp = (esp_gatt_rsp_t *)malloc(sizeof(esp_gatt_rsp_t)); - gatt_rsp->attr_value.len = param->write.len; - gatt_rsp->attr_value.handle = param->write.handle; - gatt_rsp->attr_value.offset = param->write.offset; - gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; - memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); - esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); - if (response_err != ESP_OK) { - ESP_LOGE(COEX_TAG, "Send response error"); + if (gatt_rsp) { + gatt_rsp->attr_value.len = param->write.len; + gatt_rsp->attr_value.handle = param->write.handle; + gatt_rsp->attr_value.offset = param->write.offset; + gatt_rsp->attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE; + memcpy(gatt_rsp->attr_value.value, param->write.value, param->write.len); + esp_err_t response_err = esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, status, gatt_rsp); + if (response_err != ESP_OK) { + ESP_LOGE(COEX_TAG, "Send response error\n"); + } + free(gatt_rsp); + } else { + ESP_LOGE(COEX_TAG, "%s, malloc failed", __func__); + status = ESP_GATT_NO_RESOURCES; } - free(gatt_rsp); if (status != ESP_GATT_OK) { return; }