Skip to content

Commit 1df6931

Browse files
committed
2022-12-18: Fix potential CORRUPT HEAP problem on libraries/BLE/src/BLEDevice.cpp
espressif/arduino-esp32#7597
1 parent c094cde commit 1df6931

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/BLEDevice.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
BLEServer* BLEDevice::m_pServer = nullptr;
4242
BLEScan* BLEDevice::m_pScan = nullptr;
4343
BLEClient* BLEDevice::m_pClient = nullptr;
44-
bool initialized = false;
44+
bool initialized = false;
4545
esp_ble_sec_act_t BLEDevice::m_securityLevel = (esp_ble_sec_act_t)0;
4646
BLESecurityCallbacks* BLEDevice::m_securityCallbacks = nullptr;
4747
uint16_t BLEDevice::m_localMTU = 23; // not sure if this variable is useful
@@ -348,7 +348,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr;
348348
}
349349

350350
#ifndef CONFIG_BT_CLASSIC_ENABLED
351-
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
351+
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
352352
#endif
353353
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
354354
errRc = esp_bt_controller_init(&bt_cfg);
@@ -577,7 +577,7 @@ BLEAdvertising* BLEDevice::getAdvertising() {
577577
log_i("create advertising");
578578
}
579579
log_d("get advertising");
580-
return m_bleAdvertising;
580+
return m_bleAdvertising;
581581
}
582582

583583
void BLEDevice::startAdvertising() {
@@ -629,10 +629,15 @@ void BLEDevice::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
629629
m_connectedClientsMap.insert(std::pair<uint16_t, conn_status_t>(conn_id, status));
630630
}
631631

632+
//there may have some situation that invoking this function simultaneously, that will cause CORRUPT HEAP
633+
//let this function serializable
634+
portMUX_TYPE BLEDevice::mux = portMUX_INITIALIZER_UNLOCKED;
632635
void BLEDevice::removePeerDevice(uint16_t conn_id, bool _client) {
636+
portENTER_CRITICAL(&mux);
633637
log_i("remove: %d, GATT role %s", conn_id, _client?"client":"server");
634638
if(m_connectedClientsMap.find(conn_id) != m_connectedClientsMap.end())
635639
m_connectedClientsMap.erase(conn_id);
640+
portEXIT_CRITICAL(&mux);
636641
}
637642

638643
/* multi connect support */
@@ -652,8 +657,8 @@ void BLEDevice::removePeerDevice(uint16_t conn_id, bool _client) {
652657
if (release_memory) {
653658
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM); // <-- require tests because we released classic BT memory and this can cause crash (most likely not, esp-idf takes care of it)
654659
} else {
655-
initialized = false;
656-
}
660+
initialized = false;
661+
}
657662
#endif
658663
}
659664

src/BLEDevice.h

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class BLEDevice {
7373
static BLEAdvertising* m_bleAdvertising;
7474
static esp_gatt_if_t getGattcIF();
7575
static std::map<uint16_t, conn_status_t> m_connectedClientsMap;
76+
static portMUX_TYPE mux;
7677

7778
static void gattClientEventHandler(
7879
esp_gattc_cb_event_t event,

0 commit comments

Comments
 (0)