Skip to content

Commit 6754d61

Browse files
committed
Fix race condition in NimBLEScan::clearResults.
If clear results is called from more than one task a race condition exists that may delete the same advertisedDevice twice. This prevents this by swapping with an empty vector and testing for empty before freeing the resources.
1 parent d3f34d7 commit 6754d61

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/NimBLEScan.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,15 @@ NimBLEScanResults NimBLEScan::getResults() {
468468
* @brief Clear the stored results of the scan.
469469
*/
470470
void NimBLEScan::clearResults() {
471-
for (const auto& dev : m_scanResults.m_deviceVec) {
472-
delete dev;
471+
std::vector<NimBLEAdvertisedDevice*> vSwap{};
472+
if (m_scanResults.m_deviceVec.size()) {
473+
ble_npl_hw_enter_critical();
474+
vSwap.swap(m_scanResults.m_deviceVec);
475+
ble_npl_hw_exit_critical(0);
476+
for (const auto& dev : vSwap) {
477+
delete dev;
478+
}
473479
}
474-
std::vector<NimBLEAdvertisedDevice*>().swap(m_scanResults.m_deviceVec);
475480
} // clearResults
476481

477482
/**

0 commit comments

Comments
 (0)