Skip to content

Commit

Permalink
Fix endpoint index lookups to handle invalid endpoint ids correctly. (p…
Browse files Browse the repository at this point in the history
…roject-chip#22275)

There are some code paths that loop over all endpoint indices (including ones
that do not have an endpoint defined yet) and then use the endpoint id to look
up endpoint indices.  Make sure we don't claim an endpoint index for the "not an
endpoint" endpoint id.

Also fixes findClusterEndpointIndex to check for the invalid endpoint id before
working with it, since that means the endpoint index it's looking at does not
correspond to a defined endpoint.

Fixes project-chip#22272
  • Loading branch information
bzbarsky-apple authored and isiu-apple committed Sep 16, 2022
1 parent d7e0558 commit 269ce85
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ void emberAfSetDynamicEndpointCount(uint16_t dynamicEndpointCount)

uint16_t emberAfGetDynamicIndexFromEndpoint(EndpointId id)
{
if (id == kInvalidEndpointId)
{
return kEmberInvalidEndpointIndex;
}

uint16_t index;
for (index = FIXED_ENDPOINT_COUNT; index < MAX_ENDPOINT_COUNT; index++)
{
Expand Down Expand Up @@ -831,6 +836,11 @@ static uint16_t findClusterEndpointIndex(EndpointId endpoint, ClusterId clusterI
{
break;
}
if (emAfEndpoints[i].endpoint == kInvalidEndpointId)
{
// Not actually a configured endpoint.
continue;
}
epi = static_cast<uint16_t>(
epi + ((emberAfFindClusterIncludingDisabledEndpoints(emAfEndpoints[i].endpoint, clusterId, mask) != nullptr) ? 1 : 0));
}
Expand All @@ -840,6 +850,11 @@ static uint16_t findClusterEndpointIndex(EndpointId endpoint, ClusterId clusterI

static uint16_t findIndexFromEndpoint(EndpointId endpoint, bool ignoreDisabledEndpoints)
{
if (endpoint == kInvalidEndpointId)
{
return kEmberInvalidEndpointIndex;
}

uint16_t epi;
for (epi = 0; epi < emberAfEndpointCount(); epi++)
{
Expand Down

0 comments on commit 269ce85

Please sign in to comment.