Skip to content

Safer and more efficient handling of the internal whitelist#1087

Open
doudar wants to merge 15 commits intoh2zero:masterfrom
doudar:master
Open

Safer and more efficient handling of the internal whitelist#1087
doudar wants to merge 15 commits intoh2zero:masterfrom
doudar:master

Conversation

@doudar
Copy link
Contributor

@doudar doudar commented Feb 12, 2026

This pull request makes several improvements and bug fixes to the whitelist management logic in NimBLEDevice. The changes focus on safer and more efficient handling of the internal whitelist vector, as well as correcting a boundary check bug.

Whitelist management improvements:

  • Changed usage of m_whiteList from pointer arithmetic (&m_whiteList[0]) to the safer and more idiomatic m_whiteList.data() when passing data to ble_gap_wl_set.
  • In whiteListRemove, now passes nullptr to ble_gap_wl_set when the whitelist is empty, preventing invalid pointer dereference. Also uses m_whiteList.shrink_to_fit() to optimize memory usage after removal, replacing the previous swap idiom.

Bug fix:

  • Corrected the boundary check in getWhiteListAddress from index > m_whiteList.size() to index >= m_whiteList.size(), fixing an off-by-one error when accessing whitelist entries.

NimBLECharacteristic changes:

Compacted descriptor creation path
    Simplified createDescriptor(const NimBLEUUID&, ...) to early-return for 0x2904, removing unnecessary temporary initialization and branching.

Reduced branch work in send loop
    In sendValue(...), selected notify/indicate function once before the loop:
        sendFunc = isNotification ? ble_gatts_notify_custom : ble_gatts_indicate_custom
    Reused sendFunc(...) inside the loop instead of branching every iteration.
    Marked chSpecified and requireSecure as const.

Compacted callback assignment
    Replaced multi-branch setCallbacks(...) with:
        m_pCallbacks = pCallbacks != nullptr ? pCallbacks : &defaultCallback;

Addressed code review feedback
    Renamed generic local send to sendFunc for clarity.

Validation performed

Investigated CI workflow runs via GitHub Actions MCP tools:
    list_workflow_runs
    get_workflow_run
    list_workflow_jobs
    get_job_logs
Baseline and post-change targeted build attempts using existing workflow-style PlatformIO command both failed at dependency/platform download step with:
    Platform Manager: Installing espressif32
    HTTPClientError
This indicates validation was blocked by environment/network access, not by code changes.
No UI changes were made (so no screenshot applicable).

Security Summary

Ran codeql_checker after code review.
Result: no analyzable code changes for CodeQL languages in this environment, so no findings reported.
No new dependencies were added.
No security issues introduced by the refactor.

NimBLEClient changes:

This PR addresses the request to make NimBLEClient code more compact/efficient while preserving behavior and documentation quality. The change is intentionally narrow: it refactors one hot lookup path in NimBLEClient.cpp without altering public behavior.

Service lookup micro-refactor (NimBLEClient::getService(const NimBLEUUID&))
    Cache uuid.toString() once and reuse it for both debug log statements.
    Replace repeated “vector grew, return back()” checks with a single local helper lambda.
    Keep all retrieval fallbacks (direct UUID, 128-bit conversion, 16-bit conversion) unchanged.

Behavioral scope
    No API changes.
    No callback, connection, discovery, or security flow changes.
    No semantic change to service resolution order or return conditions.

const std::string uuidStr = uuid.toString();
NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuidStr.c_str());

size_t prevSize = m_svcVec.size();
auto getLastIfAdded = this, prevSize -> NimBLERemoteService* {
return m_svcVec.size() > prevSize ? m_svcVec.back() : nullptr;
};

Copilot AI and others added 15 commits February 12, 2026 06:07
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Compact whitelist handling in NimBLEDevice
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Refactor `NimBLECharacteristic` control flow for smaller hot-path overhead
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Co-authored-by: doudar <17362216+doudar@users.noreply.github.com>
Refactor NimBLEClient service lookup path to reduce duplicate work and branch repetition
return create2904();
}

NimBLEDescriptor* pDescriptor = new NimBLEDescriptor(uuid, properties, maxLen, this);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this one

} else {
m_pCallbacks = &defaultCallback;
}
m_pCallbacks = pCallbacks != nullptr ? pCallbacks : &defaultCallback;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

@doudar
Copy link
Contributor Author

doudar commented Feb 14, 2026

@copilot , I’m not sure it’s possible, but try to make @h2zero happy by addressing his comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants