Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing nil crashes #34362

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@
"random": "cpp",
"thread": "cpp",
"variant": "cpp",
"any": "cpp"
"any": "cpp",
"future": "cpp"
},
// Configure paths or glob patterns to exclude from file watching.
"files.watcherExclude": {
Expand Down
65 changes: 34 additions & 31 deletions src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Matter/MTRClusterConstants.h>
#import <Matter/MTRDefines.h>
#import <Matter/Matter.h>

#import "MTRAttributeTLVValueDecoder_Internal.h"
#import "MTRBaseDevice_Internal.h"
Expand All @@ -31,31 +30,32 @@
#import "MTRLogging_Internal.h"
#import "MTRMetricKeys.h"
#import "MTRSetupPayload_Internal.h"
#import "MTRUtilities.h"
#import "NSDataSpanConversion.h"
#import "NSStringSpanConversion.h"
#import "zap-generated/MTRCommandPayloads_Internal.h"

#include "app/ConcreteAttributePath.h"
#include "app/ConcreteCommandPath.h"
#include "app/ConcreteEventPath.h"
#include "app/StatusResponse.h"
#include "lib/core/CHIPError.h"
#include "lib/core/DataModelTypes.h"

#include <app/AttributePathParams.h>
#include <app/BufferedReadCallback.h>
#include <app/ClusterStateCache.h>
#include <app/InteractionModelEngine.h>
#include <app/ReadClient.h>
#include <app/data-model/List.h>
#include <controller/CommissioningWindowOpener.h>
#include <controller/ReadInteraction.h>
#include <controller/WriteInteraction.h>
#include <crypto/CHIPCryptoPAL.h>
#include <setup_payload/SetupPayload.h>
#include <system/SystemClock.h>

#include <memory>
#import "app/ConcreteAttributePath.h"
#import "app/ConcreteCommandPath.h"
#import "app/ConcreteEventPath.h"
#import "app/StatusResponse.h"
#import "lib/core/CHIPError.h"
#import "lib/core/DataModelTypes.h"

#import <app/AttributePathParams.h>
#import <app/BufferedReadCallback.h>
#import <app/ClusterStateCache.h>
#import <app/InteractionModelEngine.h>
#import <app/ReadClient.h>
#import <app/data-model/List.h>
#import <controller/CommissioningWindowOpener.h>
#import <controller/ReadInteraction.h>
#import <controller/WriteInteraction.h>
#import <crypto/CHIPCryptoPAL.h>
#import <setup_payload/SetupPayload.h>
#import <system/SystemClock.h>

#import <memory>

using namespace chip;
using namespace chip::app;
Expand Down Expand Up @@ -2290,8 +2290,9 @@ + (MTRAttributeRequestPath *)requestPathWithEndpointID:(NSNumber * _Nullable)end

- (BOOL)isEqualToAttributeRequestPath:(MTRAttributeRequestPath *)path
{
return [_endpoint isEqualToNumber:path.endpoint] && [_cluster isEqualToNumber:path.cluster] &&
[_attribute isEqualToNumber:path.attribute];
return MTREqualObjects(_endpoint, path.endpoint)
&& MTREqualObjects(_cluster, path.cluster)
&& MTREqualObjects(_attribute, path.attribute);
}

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

- (BOOL)isEqualToEventRequestPath:(MTREventRequestPath *)path
{
return
[_endpoint isEqualToNumber:path.endpoint] && [_cluster isEqualToNumber:path.cluster] && [_event isEqualToNumber:path.event];
return MTREqualObjects(_endpoint, path.endpoint)
&& MTREqualObjects(_cluster, path.cluster)
&& MTREqualObjects(_event, path.event);
}

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

- (BOOL)isEqualToClusterPath:(MTRClusterPath *)clusterPath
{
return [_endpoint isEqualToNumber:clusterPath.endpoint] && [_cluster isEqualToNumber:clusterPath.cluster];
return MTREqualObjects(_endpoint, clusterPath.endpoint)
&& MTREqualObjects(_cluster, clusterPath.cluster);
}

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

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

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

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

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

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

- (BOOL)isEqual:(id)object
Expand Down
26 changes: 14 additions & 12 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

#import <Matter/MTRDefines.h>
#import <Matter/Matter.h>
#import <os/lock.h>

#import "MTRAsyncWorkQueue.h"
Expand All @@ -39,19 +39,20 @@
#import "MTRMetricsCollector.h"
#import "MTRTimeUtils.h"
#import "MTRUnfairLock.h"
#import "MTRUtilities.h"
#import "zap-generated/MTRCommandPayloads_Internal.h"

#include "lib/core/CHIPError.h"
#include "lib/core/DataModelTypes.h"
#include <app/ConcreteAttributePath.h>
#include <lib/support/FibonacciUtils.h>
#import "lib/core/CHIPError.h"
#import "lib/core/DataModelTypes.h"
#import <app/ConcreteAttributePath.h>
#import <lib/support/FibonacciUtils.h>

#include <app/AttributePathParams.h>
#include <app/BufferedReadCallback.h>
#include <app/ClusterStateCache.h>
#include <app/InteractionModelEngine.h>
#include <platform/LockTracker.h>
#include <platform/PlatformManager.h>
#import <app/AttributePathParams.h>
#import <app/BufferedReadCallback.h>
#import <app/ClusterStateCache.h>
#import <app/InteractionModelEngine.h>
#import <platform/LockTracker.h>
#import <platform/PlatformManager.h>

typedef void (^MTRDeviceAttributeReportHandler)(NSArray * _Nonnull);

Expand Down Expand Up @@ -336,7 +337,8 @@ - (id)copyWithZone:(NSZone *)zone

- (BOOL)isEqualToClusterData:(MTRDeviceClusterData *)otherClusterData
{
return [_dataVersion isEqual:otherClusterData.dataVersion] && [_attributes isEqual:otherClusterData.attributes];
return MTREqualObjects(_dataVersion, otherClusterData.dataVersion)
&& MTREqualObjects(_attributes, otherClusterData.attributes);
}

- (BOOL)isEqual:(id)object
Expand Down
13 changes: 12 additions & 1 deletion src/darwin/Framework/CHIP/MTRUtilities.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,16 @@

BOOL MTREqualObjects(id<NSObject> _Nullable a, id<NSObject> _Nullable b)
{
return (a == nil) ? (b == nil) : [a isEqual:b];
// Check if A is nil, and return based on B being nil (or not)
if (a == nil) {
return b == nil;
}

// If B is nil at this point, we're not equal
if (b == nil) {
return NO;
}

// Otherwise work on equality, given that we're both non nil
return [a isEqual:b];
}
2 changes: 1 addition & 1 deletion src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,7 @@
B2E0D7B2245B0B5C003C5B48 /* MTRManualSetupPayloadParser.h in Headers */,
3CF134A7289D8ADA0017A19E /* MTRCSRInfo.h in Headers */,
88E07D612B9A89A4005FD53E /* MTRMetricKeys.h in Headers */,
3D4733B32BE2D1DA003DC19B /* MTRUtilities.h in Headers */,
B2E0D7B1245B0B5C003C5B48 /* Matter.h in Headers */,
7596A84428762729004DAE0E /* MTRDevice.h in Headers */,
B2E0D7B8245B0B5C003C5B48 /* MTRSetupPayload.h in Headers */,
Expand Down Expand Up @@ -1671,7 +1672,6 @@
514C7A022B64223400DD6D7B /* MTRServerEndpoint_Internal.h in Headers */,
511913FC28C100EF009235E9 /* MTRBaseSubscriptionCallback.h in Headers */,
51565CB12A7AD77600469F18 /* MTRDeviceControllerDataStore.h in Headers */,
3D4733B32BE2D1DA003DC19B /* MTRUtilities.h in Headers */,
3D843713294977000070D20A /* NSDataSpanConversion.h in Headers */,
991DC08B247704DC00C13860 /* MTRLogging_Internal.h in Headers */,
51FE723F2ACDEF3E00437032 /* MTRCommandPayloadExtensions_Internal.h in Headers */,
Expand Down
Loading