fix(ble): NimBLE v2 scan broken, pRxCharacteristic null, destructor UB, size_t underflow, Cardputer display offset#2465
Open
Swissola wants to merge 1 commit into
Open
Conversation
ble_common.cpp — NimBLE v2+ scan registers base NimBLEScanCallbacks The legacy path wires AdvertisedDeviceCallbacks (the subclass that populates the device list). The v2+ path wired the empty base class directly, so the BLE scanner found nothing on any NimBLE v2+ build. Fix: use AdvertisedDeviceCallbacks on both paths. ble_common.cpp — pRxCharacteristic local variable shadows module global initBLEServer() re-declared BLECharacteristic *pRxCharacteristic as a local, leaving the module-level global always null. Any code outside the function using the global would dereference a null pointer. Fix: remove the type declaration so the assignment targets the global. ble_common.cpp — explicit ~NimBLEService() destructor call is UB The service is owned by the NimBLE stack; calling its destructor manually corrupts the stack's service table. BLEDevice::deinit() on the next line already tears down the entire stack including services. Fix: remove the manual destructor call. BLE_Suite.cpp — size_t underflow when deviceAddresses is empty The bubble-sort loop computed size_t(0) - 1 == SIZE_MAX, causing an always-true loop condition and an immediate out-of-bounds access. Fix: guard the outer loop with a size() > 1 pre-check. BLE_Suite.cpp — scan progress text rendered off-screen on Cardputer Hardcoded y=120/y=140 for scan status messages. The Cardputer display is 135px tall; y=140 places text 5px below the bottom edge. Fix: use tftHeight-relative offsets (tftHeight-30, tftHeight-15). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Five bugs in the BLE module, all found by reading the source. PRs against
devas per contributing guidelines.1. NimBLE v2+ scan always finds nothing
ble_scan_setup()registered the baseNimBLEScanCallbacksclass on the v2+ path instead of theAdvertisedDeviceCallbackssubclass. The base class has an emptyonResult(), so no devices are ever added tooptions. The BLE scanner menu shows only "Back" on any NimBLE v2+ build.2.
pRxCharacteristiclocal variable shadows module globalinitBLEServer()declaredBLECharacteristic *pRxCharacteristic = ...as a local, shadowing the module-level global on line 21. The global staysnullptrfor the lifetime of the program. Any code that uses the global afterinitBLEServer()returns dereferences null.3. Explicit
~NimBLEService()destructor call is undefined behaviourdisPlayBLESend()calledpService->~NimBLEService()directly. The service is owned by the NimBLE stack; manual destructor invocation corrupts the stack's internal service table without freeing memory.BLEDevice::deinit()on the next line already performs a full teardown — the explicit call is both wrong and redundant.4.
size_tunderflow crashes sort when device list is emptyThe bubble-sort in
selectTargetFromScan()computedscannerData.deviceAddresses.size() - 1assize_t. When the vector is empty,size_t(0) - 1wraps toSIZE_MAX, making the loop condition always true and immediately causing an out-of-bounds vector access.5. Scan progress text rendered off-screen on Cardputer (135 px tall display)
setCursor(20, 140)for "Passive scan (15s)..." places text 5 px below the bottom of the Cardputer's 135 px landscape display. Changed totftHeight-relative offsets, which are already used throughout the rest of the file.Files changed
src/modules/ble/ble_common.cpp— bugs 1, 2, 3src/modules/ble/BLE_Suite.cpp— bugs 4, 5🤖 Generated with Claude Code