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.
This commit is contained in:
h2zero 2025-01-12 16:33:19 -07:00 committed by Ryan Powell
parent a9578637cb
commit e6249623d5

View file

@ -468,10 +468,15 @@ NimBLEScanResults NimBLEScan::getResults() {
* @brief Clear the stored results of the scan. * @brief Clear the stored results of the scan.
*/ */
void NimBLEScan::clearResults() { void NimBLEScan::clearResults() {
for (const auto& dev : m_scanResults.m_deviceVec) { if (m_scanResults.m_deviceVec.size()) {
delete dev; 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 } // clearResults
/** /**