Skip to content

Commit

Permalink
Fixing crashes when comparing objects to nil (project-chip#34321)
Browse files Browse the repository at this point in the history
* Fixing nil check crashes

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and j-ororke committed Aug 14, 2024
1 parent 5d96f10 commit 9b356f5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
40 changes: 29 additions & 11 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2290,9 +2290,12 @@ + (MTRAttributeRequestPath *)requestPathWithEndpointID:(NSNumber * _Nullable)end

- (BOOL)isEqualToAttributeRequestPath:(MTRAttributeRequestPath *)path
{
return MTREqualObjects(_endpoint, path.endpoint)
&& MTREqualObjects(_cluster, path.cluster)
&& MTREqualObjects(_attribute, path.attribute);
if (!path)
return NO;

return (path.endpoint && [_endpoint isEqualToNumber:path.endpoint])
&& (path.cluster && [_cluster isEqualToNumber:path.cluster])
&& (path.attribute && [_attribute isEqualToNumber:path.attribute]);
}

- (BOOL)isEqual:(id)object
Expand Down Expand Up @@ -2363,9 +2366,12 @@ + (MTREventRequestPath *)requestPathWithEndpointID:(NSNumber * _Nullable)endpoin

- (BOOL)isEqualToEventRequestPath:(MTREventRequestPath *)path
{
return MTREqualObjects(_endpoint, path.endpoint)
&& MTREqualObjects(_cluster, path.cluster)
&& MTREqualObjects(_event, path.event);
if (!path)
return NO;

return (path.endpoint && [_endpoint isEqualToNumber:path.endpoint])
&& (path.cluster && [_cluster isEqualToNumber:path.cluster])
&& (path.event && [_event isEqualToNumber:path.event]);
}

- (BOOL)isEqual:(id)object
Expand Down Expand Up @@ -2434,8 +2440,11 @@ ConcreteClusterPath path(static_cast<chip::EndpointId>([endpointID unsignedShort

- (BOOL)isEqualToClusterPath:(MTRClusterPath *)clusterPath
{
return MTREqualObjects(_endpoint, clusterPath.endpoint)
&& MTREqualObjects(_cluster, clusterPath.cluster);
if (!clusterPath)
return NO;

return (clusterPath.endpoint && [_endpoint isEqualToNumber:clusterPath.endpoint])
&& (clusterPath.cluster && [_cluster isEqualToNumber:clusterPath.cluster]);
}

- (BOOL)isEqual:(id)object
Expand Down Expand Up @@ -2523,7 +2532,10 @@ ConcreteDataAttributePath path(static_cast<chip::EndpointId>([endpointID unsigne

- (BOOL)isEqualToAttributePath:(MTRAttributePath *)attributePath
{
return [self isEqualToClusterPath:attributePath] && MTREqualObjects(_attribute, attributePath.attribute);
if (!attributePath)
return NO;

return [self isEqualToClusterPath:attributePath] && attributePath.attribute && [_attribute isEqualToNumber:attributePath.attribute];
}

- (BOOL)isEqual:(id)object
Expand Down Expand Up @@ -2616,7 +2628,10 @@ ConcreteEventPath path(static_cast<chip::EndpointId>([endpointID unsignedShortVa

- (BOOL)isEqualToEventPath:(MTREventPath *)eventPath
{
return [self isEqualToClusterPath:eventPath] && MTREqualObjects(_event, eventPath.event);
if (!eventPath)
return NO;

return [self isEqualToClusterPath:eventPath] && eventPath.event && [_event isEqualToNumber:eventPath.event];
}

- (BOOL)isEqual:(id)object
Expand Down Expand Up @@ -2706,7 +2721,10 @@ ConcreteCommandPath path(static_cast<chip::EndpointId>([endpointID unsignedShort

- (BOOL)isEqualToCommandPath:(MTRCommandPath *)commandPath
{
return [self isEqualToClusterPath:commandPath] && MTREqualObjects(_command, commandPath.command);
if (!commandPath)
return NO;

return [self isEqualToClusterPath:commandPath] && commandPath.command && [_command isEqualToNumber:commandPath.command];
}

- (BOOL)isEqual:(id)object
Expand Down
7 changes: 5 additions & 2 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,11 @@ - (id)copyWithZone:(NSZone *)zone

- (BOOL)isEqualToClusterData:(MTRDeviceClusterData *)otherClusterData
{
return MTREqualObjects(_dataVersion, otherClusterData.dataVersion)
&& MTREqualObjects(_attributes, otherClusterData.attributes);
if (!otherClusterData)
return NO;

return (otherClusterData.dataVersion && [_dataVersion isEqual:otherClusterData.dataVersion])
&& (otherClusterData.attributes && [_attributes isEqual:otherClusterData.attributes]);
}

- (BOOL)isEqual:(id)object
Expand Down

0 comments on commit 9b356f5

Please sign in to comment.