fix(ble): Fix Advertising data being overwritten #12096
Open
+151
−8
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.
Description of Change
This pull request enhances memory management and object copying for the
BLEAdvertisedDeviceclass, ensuring safe handling of the advertising payload buffer. The changes add a destructor, copy constructor, and assignment operator, and improve how the payload buffer is allocated, copied, and freed to avoid memory leaks and ensure correct behavior when objects are copied or reassigned.Memory management and object lifecycle improvements:
BLEAdvertisedDeviceto properly manage the dynamicm_payloadbuffer, ensuring deep copies and freeing memory when needed. (libraries/BLE/src/BLEAdvertisedDevice.cpp,libraries/BLE/src/BLEAdvertisedDevice.h) [1] [2]m_payloadandm_payloadLengthto safe default values. (libraries/BLE/src/BLEAdvertisedDevice.cpp)Payload buffer handling improvements:
parseAdvertisementto always make a copy of the payload buffer and to append scan response data safely, preventing use-after-free or double-free issues. (libraries/BLE/src/BLEAdvertisedDevice.cpp)setPayloadto allocate, copy, and append payload data safely, with error checking and freeing of previous buffers as needed. (libraries/BLE/src/BLEAdvertisedDevice.cpp)Test Scenarios
Tested locally
Related links
Closes #12075