From bb95a1d62243dc3f4ce7c208d68f50cb57713eab Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Wed, 7 Jun 2017 09:07:26 -0700 Subject: [PATCH] Fix loops iteration The erase() already increment the iterator. If the loop additionally do "it++", it will skip some elements, and might iterate over the end. Test: compilation test Change-Id: Ibd9c993d8e80d807f5d7bc920da36ee538477438 --- stack/gatt/gatt_utils.cc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/stack/gatt/gatt_utils.cc b/stack/gatt/gatt_utils.cc index 7ff14131e..8cb27451d 100644 --- a/stack/gatt/gatt_utils.cc +++ b/stack/gatt/gatt_utils.cc @@ -241,10 +241,13 @@ std::list::iterator gatt_find_hdl_buffer_by_app_id( * ID. */ void gatt_free_srvc_db_buffer_app_id(tBT_UUID* p_app_id) { - auto end_it = gatt_cb.hdl_list_info->end(); - for (auto it = gatt_cb.hdl_list_info->begin(); it != end_it; it++) { + auto it = gatt_cb.hdl_list_info->begin(); + auto end = gatt_cb.hdl_list_info->end(); + while (it != end) { if (memcmp(p_app_id, &it->asgn_range.app_uuid128, sizeof(tBT_UUID)) == 0) { it = gatt_cb.hdl_list_info->erase(it); + } else { + it++; } } } @@ -1593,14 +1596,18 @@ bool gatt_remove_bg_dev_from_list(tGATT_REG* p_reg, BD_ADDR bd_addr) { } /** deregister all related back ground connetion device. */ void gatt_deregister_bgdev_list(tGATT_IF gatt_if) { + auto it = gatt_cb.bgconn_dev.begin(); + auto end = gatt_cb.bgconn_dev.end(); /* update the BG conn device list */ - for (auto it = gatt_cb.bgconn_dev.begin(); it != gatt_cb.bgconn_dev.end(); - it++) { + while (it != end) { it->gatt_if.erase(gatt_if); - if (it->gatt_if.size() == 0) { - BTM_BleUpdateBgConnDev(false, it->remote_bda); - it = gatt_cb.bgconn_dev.erase(it); + if (it->gatt_if.size()) { + it++; + continue; } + + BTM_BleUpdateBgConnDev(false, it->remote_bda); + it = gatt_cb.bgconn_dev.erase(it); } } -- 2.11.0