Skip to content

Commit

Permalink
Fix race condition in NimBLEScan::clearResults.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
h2zero committed Jan 13, 2025
1 parent d3f34d7 commit cbbbbb2
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/NimBLEScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,10 +468,15 @@ NimBLEScanResults NimBLEScan::getResults() {
* @brief Clear the stored results of the scan.
*/
void NimBLEScan::clearResults() {
for (const auto& dev : m_scanResults.m_deviceVec) {
delete dev;
if (m_scanResults.m_deviceVec.size()) {
std::vector<NimBLEAdvertisedDevice*> vSwap{};
ble_npl_hw_enter_critical();
vSwap.swap(m_scanResults.m_deviceVec);
ble_npl_hw_exit_critical(0);
for (const auto& dev : vSwap) {
delete dev;
}
}
std::vector<NimBLEAdvertisedDevice*>().swap(m_scanResults.m_deviceVec);
} // clearResults

/**
Expand Down

0 comments on commit cbbbbb2

Please sign in to comment.