Skip to content

Commit

Permalink
Fix crash when retrieving descriptors.
Browse files Browse the repository at this point in the history
If more than one descriptor is found the task would be released beofore the process completed causing a race condition.
  • Loading branch information
h2zero committed Jan 6, 2025
1 parent e374f50 commit 74ac317
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/NimBLERemoteCharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,8 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(
const auto pChr = (NimBLERemoteCharacteristic*)pTaskData->m_pInstance;
const NimBLEUUID* uuidFilter = filter->uuid;

if (error->status == BLE_HS_ENOTCONN) {
NIMBLE_LOGE(LOG_TAG, "<< Descriptor Discovery; Not connected");
NimBLEUtils::taskRelease(*pTaskData, error->status);
return error->status;
}

if (pChr->getHandle() != chr_val_handle) {
rc = BLE_HS_EDONE; // descriptor not for this characteristic
return 0; // Descriptor not for this characteristic
}

if (rc == 0) {
Expand All @@ -92,6 +86,7 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(
}

pChr->m_vDescriptors.push_back(new NimBLERemoteDescriptor(pChr, dsc));
return 0;
}

NimBLEUtils::taskRelease(*pTaskData, rc);
Expand Down
5 changes: 4 additions & 1 deletion src/NimBLERemoteService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t conn_handle,
const ble_gatt_error* error,
const ble_gatt_chr* chr,
void* arg) {
NIMBLE_LOGD(LOG_TAG, "Characteristic Discovery >>");
NIMBLE_LOGD(LOG_TAG,
"Characteristic Discovery >> status: %d handle: %d",
error->status,
(error->status == 0) ? chr->def_handle : -1);
auto pTaskData = (NimBLETaskData*)arg;
const auto pSvc = (NimBLERemoteService*)pTaskData->m_pInstance;

Expand Down

0 comments on commit 74ac317

Please sign in to comment.