project-chip/connectedhomeip

[BUG] Inconsistent State of mDevices Array on Error Return in Bridge Example

Closed this issue · 0 comments

Reproduction steps

Issue Summary: Inconsistent State of mDevices Array on Error Return
Description
In the current implementation of the DeviceManager::AddDeviceEndpoint function, there is a potential issue where the mDevices array can be left in an inconsistent state when errors occur. Specifically, when the function encounters an error and returns -1, it does not reset the mDevices[index] element back to nullptr. This can lead to the array containing invalid pointers, which can cause undefined behavior in subsequent operations.

Affected Function
DeviceManager::AddDeviceEndpoint

Details
The function iterates through mDevices array to find an available slot (i.e., where mDevices[index] is nullptr).
Upon finding an available slot, it attempts to add a device endpoint.
If an error occurs (other than CHIP_ERROR_ENDPOINT_EXISTS), the function returns -1 but does not reset mDevices[index] to nullptr.
If all retries are exhausted or no endpoints are available, the function similarly returns -1 without resetting mDevices[index] to nullptr.
Proposed Fix
Modify the DeviceManager::AddDeviceEndpoint function to ensure mDevices[index] is set back to nullptr in all cases where the function returns -1 due to an error. This ensures the mDevices array remains consistent and does not contain invalid pointers.

Bug prevalence

1-2 times week

GitHub hash of the SDK that was being used

1d4ac45

Platform

other

Platform Version(s)

No response

Anything else?

No response