Skip to content

Commit 915fb2c

Browse files
authored
[Darwin] Some MTRDevice mtr_strongify sites need to guard against nil self (project-chip#36131)
* [Darwin] Some MTRDevice mtr_strongify sites need to guard against nil self * Took ksperling-apple's suggestion to use VerifyOrReturn instead for cleaner code
1 parent 3a6c545 commit 915fb2c

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+18-8
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ - (instancetype)initWithNodeID:(NSNumber *)nodeID controller:(MTRDeviceControlle
389389
mtr_weakify(self);
390390
_systemTimeChangeObserverToken = [[NSNotificationCenter defaultCenter] addObserverForName:NSSystemClockDidChangeNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull notification) {
391391
mtr_strongify(self);
392+
VerifyOrReturn(self);
393+
392394
std::lock_guard lock(self->_lock);
393395
[self _resetStorageBehaviorState];
394396
}];
@@ -760,6 +762,8 @@ - (void)_ensureSubscriptionForExistingDelegates:(NSString *)reason
760762
mtr_weakify(self);
761763
[self _scheduleSubscriptionPoolWork:^{
762764
mtr_strongify(self);
765+
VerifyOrReturn(self);
766+
763767
[self->_deviceController asyncDispatchToMatterQueue:^{
764768
std::lock_guard lock(self->_lock);
765769
[self _setupSubscriptionWithReason:[NSString stringWithFormat:@"%@ and scheduled subscription is happening", reason]];
@@ -1168,16 +1172,16 @@ - (void)_scheduleSubscriptionPoolWork:(dispatch_block_t)workBlock inNanoseconds:
11681172
mtr_weakify(self);
11691173
dispatch_block_t workBlockToQueue = ^{
11701174
mtr_strongify(self);
1171-
if (nil == self) {
1172-
// This block may be delayed by a specified number of nanoseconds, potentially running after the device is deallocated.
1173-
// If so, MTRAsyncWorkItem::initWithQueue will assert on a nil queue, which will cause a crash.
1174-
return;
1175-
}
1175+
// This block may be delayed by a specified number of nanoseconds, potentially running after the device is deallocated.
1176+
// If so, MTRAsyncWorkItem::initWithQueue will assert on a nil queue, which will cause a crash.
1177+
VerifyOrReturn(self);
11761178

11771179
// In the case where a resubscription triggering event happened and already established, running the work block should result in a no-op
11781180
MTRAsyncWorkItem * workItem = [[MTRAsyncWorkItem alloc] initWithQueue:self.queue];
11791181
[workItem setReadyHandler:^(id _Nonnull context, NSInteger retryCount, MTRAsyncWorkCompletionBlock _Nonnull completion) {
11801182
mtr_strongify(self);
1183+
VerifyOrReturn(self);
1184+
11811185
MTR_LOG("%@ - work item is ready to attempt pooled subscription", self);
11821186
os_unfair_lock_lock(&self->_lock);
11831187
#ifdef DEBUG
@@ -1257,6 +1261,8 @@ - (void)_handleResubscriptionNeededWithDelayOnDeviceQueue:(NSNumber *)resubscrip
12571261
mtr_weakify(self);
12581262
auto resubscriptionBlock = ^{
12591263
mtr_strongify(self);
1264+
VerifyOrReturn(self);
1265+
12601266
[self->_deviceController asyncDispatchToMatterQueue:^{
12611267
[self _triggerResubscribeWithReason:@"ResubscriptionNeeded timer fired" nodeLikelyReachable:NO];
12621268
} errorHandler:^(NSError * _Nonnull error) {
@@ -1369,6 +1375,8 @@ - (void)_doHandleSubscriptionReset:(NSNumber * _Nullable)retryDelay
13691375
mtr_weakify(self);
13701376
auto resubscriptionBlock = ^{
13711377
mtr_strongify(self);
1378+
VerifyOrReturn(self);
1379+
13721380
[self->_deviceController asyncDispatchToMatterQueue:^{
13731381
std::lock_guard lock(self->_lock);
13741382
[self _reattemptSubscriptionNowIfNeededWithReason:@"got subscription reset"];
@@ -2386,9 +2394,9 @@ - (void)_setupSubscriptionWithReason:(NSString *)reason
23862394
mtr_weakify(self);
23872395
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, static_cast<int64_t>(kSecondsToWaitBeforeMarkingUnreachableAfterSettingUpSubscription) * static_cast<int64_t>(NSEC_PER_SEC)), self.queue, ^{
23882396
mtr_strongify(self);
2389-
if (self != nil) {
2390-
[self _markDeviceAsUnreachableIfNeverSubscribed];
2391-
}
2397+
VerifyOrReturn(self);
2398+
2399+
[self _markDeviceAsUnreachableIfNeverSubscribed];
23922400
});
23932401
}
23942402

@@ -3242,6 +3250,8 @@ - (void)_checkExpiredExpectedValues
32423250
mtr_weakify(self);
32433251
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (waitTime * NSEC_PER_SEC)), self.queue, ^{
32443252
mtr_strongify(self);
3253+
VerifyOrReturn(self);
3254+
32453255
[self _performScheduledExpirationCheck];
32463256
});
32473257
}

0 commit comments

Comments
 (0)