From 726ad9fcec5ce22b985b51f90ea2146bc7ecc94b Mon Sep 17 00:00:00 2001 From: Justin Wood Date: Fri, 12 Jul 2024 10:37:47 -1000 Subject: [PATCH] Fixing crashes when comparing objects to nil (#34321) * Fixing nil check crashes * Restyled by clang-format --------- Co-authored-by: Restyled.io --- src/darwin/Framework/CHIP/MTRBaseDevice.mm | 37 +++++++++++++++++----- src/darwin/Framework/CHIP/MTRDevice.mm | 6 +++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index 331d61129b9daf..da40f1ca24d9ac 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -2290,8 +2290,12 @@ + (MTRAttributeRequestPath *)requestPathWithEndpointID:(NSNumber * _Nullable)end - (BOOL)isEqualToAttributeRequestPath:(MTRAttributeRequestPath *)path { - return [_endpoint isEqualToNumber:path.endpoint] && [_cluster isEqualToNumber:path.cluster] && - [_attribute isEqualToNumber: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 @@ -2362,8 +2366,12 @@ + (MTREventRequestPath *)requestPathWithEndpointID:(NSNumber * _Nullable)endpoin - (BOOL)isEqualToEventRequestPath:(MTREventRequestPath *)path { - return - [_endpoint isEqualToNumber:path.endpoint] && [_cluster isEqualToNumber:path.cluster] && [_event isEqualToNumber: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 @@ -2432,7 +2440,11 @@ ConcreteClusterPath path(static_cast([endpointID unsignedShort - (BOOL)isEqualToClusterPath:(MTRClusterPath *)clusterPath { - return [_endpoint isEqualToNumber:clusterPath.endpoint] && [_cluster isEqualToNumber:clusterPath.cluster]; + if (!clusterPath) + return NO; + + return (clusterPath.endpoint && [_endpoint isEqualToNumber:clusterPath.endpoint]) + && (clusterPath.cluster && [_cluster isEqualToNumber:clusterPath.cluster]); } - (BOOL)isEqual:(id)object @@ -2520,7 +2532,10 @@ ConcreteDataAttributePath path(static_cast([endpointID unsigne - (BOOL)isEqualToAttributePath:(MTRAttributePath *)attributePath { - return [self isEqualToClusterPath:attributePath] && [_attribute isEqualToNumber:attributePath.attribute]; + if (!attributePath) + return NO; + + return [self isEqualToClusterPath:attributePath] && attributePath.attribute && [_attribute isEqualToNumber:attributePath.attribute]; } - (BOOL)isEqual:(id)object @@ -2613,7 +2628,10 @@ ConcreteEventPath path(static_cast([endpointID unsignedShortVa - (BOOL)isEqualToEventPath:(MTREventPath *)eventPath { - return [self isEqualToClusterPath:eventPath] && [_event isEqualToNumber:eventPath.event]; + if (!eventPath) + return NO; + + return [self isEqualToClusterPath:eventPath] && eventPath.event && [_event isEqualToNumber:eventPath.event]; } - (BOOL)isEqual:(id)object @@ -2703,7 +2721,10 @@ ConcreteCommandPath path(static_cast([endpointID unsignedShort - (BOOL)isEqualToCommandPath:(MTRCommandPath *)commandPath { - return [self isEqualToClusterPath:commandPath] && [_command isEqualToNumber:commandPath.command]; + if (!commandPath) + return NO; + + return [self isEqualToClusterPath:commandPath] && commandPath.command && [_command isEqualToNumber:commandPath.command]; } - (BOOL)isEqual:(id)object diff --git a/src/darwin/Framework/CHIP/MTRDevice.mm b/src/darwin/Framework/CHIP/MTRDevice.mm index fff962dcc94188..eb490bdbe0d37c 100644 --- a/src/darwin/Framework/CHIP/MTRDevice.mm +++ b/src/darwin/Framework/CHIP/MTRDevice.mm @@ -336,7 +336,11 @@ - (id)copyWithZone:(NSZone *)zone - (BOOL)isEqualToClusterData:(MTRDeviceClusterData *)otherClusterData { - return [_dataVersion isEqual:otherClusterData.dataVersion] && [_attributes isEqual:otherClusterData.attributes]; + if (!otherClusterData) + return NO; + + return (otherClusterData.dataVersion && [_dataVersion isEqual:otherClusterData.dataVersion]) + && (otherClusterData.attributes && [_attributes isEqual:otherClusterData.attributes]); } - (BOOL)isEqual:(id)object